c++ string与wstring转换

c++
wstring转string
与string转wstring

string ws2s(const wstring& ws) {
    const char* curLocale = setlocale(LC_ALL, NULL);
    setlocale(LC_ALL, "en_US.UTF-8");

    size_t _Dsize = 4 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, ws.c_str(), _Dsize);
    string result = _Dest;
    delete []_Dest;

    setlocale(LC_ALL, curLocale);
    return result;
}

wstring s2ws(const string& s) {
    const char* curLocale = setlocale(LC_ALL, NULL);
    setlocale(LC_ALL, "en_US.UTF-8");

    size_t _Dsize = s.size() + 1;
    wchar_t *_Dest = new wchar_t[_Dsize];
    wmemset(_Dest, 0, _Dsize);
    mbstowcs(_Dest, s.c_str(), _Dsize);
    wstring result = _Dest;
    delete []_Dest;

    setlocale(LC_ALL, curLocale);
    return result;
}

你可能感兴趣的:(c++ string与wstring转换)