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

后端 未结 16 2185
难免孤独
难免孤独 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:44

    method s2ws works well. Hope helps.

    std::wstring s2ws(const std::string& s) {
        std::string curLocale = setlocale(LC_ALL, ""); 
        const char* _Source = s.c_str();
        size_t _Dsize = mbstowcs(NULL, _Source, 0) + 1;
        wchar_t *_Dest = new wchar_t[_Dsize];
        wmemset(_Dest, 0, _Dsize);
        mbstowcs(_Dest,_Source,_Dsize);
        std::wstring result = _Dest;
        delete []_Dest;
        setlocale(LC_ALL, curLocale.c_str());
        return result;
    }
    

提交回复
热议问题