How to convert UTF-8 std::string to UTF-16 std::wstring?

后端 未结 6 796
挽巷
挽巷 2020-11-29 07:21

If I have a UTF-8 std::string how do I convert it to a UTF-16 std::wstring? Actually, I want to compare two Persian words.

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 07:53

    This page also seems useful: http://www.codeproject.com/KB/string/UtfConverter.aspx

    In the comment section of that page, there are also some interesting suggestions for this task like:

    // Get en ASCII std::string from anywhere
    std::string sLogLevelA = "Hello ASCII-world!";
    
    std::wstringstream ws;
    ws << sLogLevelA.c_str();
    std::wstring sLogLevel = ws.str();
    

    Or

    // To std::string:
    str.assign(ws.begin(), ws.end());
    // To std::wstring
    ws.assign(str.begin(), str.end());
    

    Though I'm not sure the validity of these approaches...

提交回复
热议问题