UTF8 to/from wide char conversion in STL

后端 未结 10 718
面向向阳花
面向向阳花 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:14

    There are several ways to do this, but the results depend on what the character encodings are in the string and wstring variables.

    If you know the string is ASCII, you can simply use wstring's iterator constructor:

    string s = "This is surely ASCII.";
    wstring w(s.begin(), s.end());
    

    If your string has some other encoding, however, you'll get very bad results. If the encoding is Unicode, you could take a look at the ICU project, which provides a cross-platform set of libraries that convert to and from all sorts of Unicode encodings.

    If your string contains characters in a code page, then may $DEITY have mercy on your soul.

提交回复
热议问题