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

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

    Here's a way to combining string, wstring and mixed string constants to wstring. Use the wstringstream class.

    This does NOT work for multi-byte character encodings. This is just a dumb way of throwing away type safety and expanding 7 bit characters from std::string into the lower 7 bits of each character of std:wstring. This is only useful if you have a 7-bit ASCII strings and you need to call an API that requires wide strings.

    #include 
    
    std::string narrow = "narrow";
    std::wstring wide = L"wide";
    
    std::wstringstream cls;
    cls << " abc " << narrow.c_str() << L" def " << wide.c_str();
    std::wstring total= cls.str();
    

提交回复
热议问题