Home >Backend Development >C++ >How Can I Safely Convert a UTF-8 String to a wstring in C ?
C : Converting a string to a wstring while Preserving Encoding
You are given a string s containing Japanese characters and wish to assign its contents to a wstring, ws. The task is to perform this assignment without introducing any encoding issues.
Solution:
Utilizing C 11 and above, this task can be accomplished solely with the standard library.
Step 1: Include Necessary Headers
#include <locale> #include <codecvt> #include <string>
Step 2: Create a converter
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
This converter will handle the conversion between UTF-8 (the encoding of s) and UTF-16 (the encoding of ws).
Step 3: Convert the string to a wstring
std::wstring ws = converter.from_bytes(s);
Example:
std::string s = "おはよう"; std::wstring ws; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; ws = converter.from_bytes(s);
Note:
The above is the detailed content of How Can I Safely Convert a UTF-8 String to a wstring in C ?. For more information, please follow other related articles on the PHP Chinese website!