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

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

    Based upon my own testing (On windows 8, vs2010) mbstowcs can actually damage original string, it works only with ANSI code page. If MultiByteToWideChar/WideCharToMultiByte can also cause string corruption - but they tends to replace characters which they don't know with '?' question marks, but mbstowcs tends to stop when it encounters unknown character and cut string at that very point. (I have tested Vietnamese characters on finnish windows).

    So prefer Multi*-windows api function over analogue ansi C functions.

    Also what I've noticed shortest way to encode string from one codepage to another is not use MultiByteToWideChar/WideCharToMultiByte api function calls but their analogue ATL macros: W2A / A2W.

    So analogue function as mentioned above would sounds like:

    wstring utf8toUtf16(const string & str)
    {
       USES_CONVERSION;
       _acp = CP_UTF8;
       return A2W( str.c_str() );
    }
    

    _acp is declared in USES_CONVERSION macro.

    Or also function which I often miss when performing old data conversion to new one:

    string ansi2utf8( const string& s )
    {
       USES_CONVERSION;
       _acp = CP_ACP;
       wchar_t* pw = A2W( s.c_str() );
    
       _acp = CP_UTF8;
       return W2A( pw );
    }
    

    But please notice that those macro's use heavily stack - don't use for loops or recursive loops for same function - after using W2A or A2W macro - better to return ASAP, so stack will be freed from temporary conversion.

提交回复
热议问题