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

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

    If you have QT and if you are lazy to implement a function and stuff you can use

    std::string str;
    QString(str).toStdWString()
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-22 06:44

    Your question is underspecified. Strictly, that example is a syntax error. However, std::mbstowcs is probably what you're looking for.

    It is a C-library function and operates on buffers, but here's an easy-to-use idiom, courtesy of TBohne (formerly Mooing Duck):

    std::wstring ws(s.size(), L' '); // Overestimate number of code points.
    ws.resize(std::mbstowcs(&ws[0], s.c_str(), s.size())); // Shrink to fit.
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 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 <sstream>
    
    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();
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题