C++ Convert string (or char*) to wstring (or wchar_t*)

后端 未结 16 2391
难免孤独
难免孤独 2020-11-22 05:57
string s = \"おはよう\";
wstring ws = FUNCTION(s, ws);

How would i assign the contents of s to ws?

Searched google and used some techniques but

16条回答
  •  迷失自我
    2020-11-22 06:35

    String to wstring

    std::wstring Str2Wstr(const std::string& str)
    {
        int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
        std::wstring wstrTo(size_needed, 0);
        MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
        return wstrTo;
    }
    

    wstring to String

    std::string Wstr2Str(const std::wstring& wstr)
    {
        typedef std::codecvt_utf8 convert_typeX;
        std::wstring_convert converterX;
        return converterX.to_bytes(wstr);
    }
    

提交回复
热议问题