Platform-Independent UTF8 and Wide Character Conversion in STL
Converting between UTF8 strings in std::string and std::wstring is a common requirement when working with wide characters. While platform-specific functions like MultiByteToWideChar exist, they are not suitable for code targeting multiple operating systems.
Solution for C 11 and Later
In C 11, the standard library introduced support for character conversion via std::codecvt. This allows for platform-independent UTF8 conversion using the following code:
// UTF-8 to UTF-16 std::string utf8Source; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; std::u16string utf16Dest = convert.from_bytes(utf8Source); // UTF-16 to UTF-8 std::u16string utf16Source; std::string utf8Dest = convert.to_bytes(utf16Source);
Other Approaches
As noted by other answers, alternative approaches exist:
Choosing the Right Approach
The choice of conversion method depends on the project's requirements and dependencies. C 11's std::codecvt is recommended for projects that prioritize simplicity and portability. For complex or platform-specific needs, consider Boost.Locale or iconv.
The above is the detailed content of How to Perform Platform-Independent UTF8 and Wide Character Conversion in C ?. For more information, please follow other related articles on the PHP Chinese website!