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

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

    use this code to convert your string to wstring

    std::wstring string2wString(const std::string& s){
        int len;
        int slength = (int)s.length() + 1;
        len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
        wchar_t* buf = new wchar_t[len];
        MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
        std::wstring r(buf);
        delete[] buf;
        return r;
    }
    
    int main(){
        std::wstring str="your string";
        std::wstring wStr=string2wString(str);
        return 0;
    }
    

提交回复
热议问题