UTF8 to/from wide char conversion in STL

后端 未结 10 908
面向向阳花
面向向阳花 2020-11-22 06:48

Is it possible to convert UTF8 string in a std::string to std::wstring and vice versa in a platform independent manner? In a Windows application I would use MultiByteToWideC

10条回答
  •  醉梦人生
    2020-11-22 07:19

    I've asked this question 5 years ago. This thread was very helpful for me back then, I came to a conclusion, then I moved on with my project. It is funny that I needed something similar recently, totally unrelated to that project from the past. As I was researching for possible solutions, I stumbled upon my own question :)

    The solution I chose now is based on C++11. The boost libraries that Constantin mentions in his answer are now part of the standard. If we replace std::wstring with the new string type std::u16string, then the conversions will look like this:

    UTF-8 to UTF-16

    std::string source;
    ...
    std::wstring_convert,char16_t> convert;
    std::u16string dest = convert.from_bytes(source);    
    

    UTF-16 to UTF-8

    std::u16string source;
    ...
    std::wstring_convert,char16_t> convert;
    std::string dest = convert.to_bytes(source);    
    

    As seen from the other answers, there are multiple approaches to the problem. That's why I refrain from picking an accepted answer.

提交回复
热议问题