std::wstring主要用於 UTF-16編碼的字符, std::string主要用於存儲單字節的字符( ASCII字符集 ),可是也能夠用來保存UTF-8編碼的字符。 UTF-8和UTF-16是UNICODE字符集的兩種不一樣的字符編碼。
std::string ws2s(const std::wstring &ws) { size_t i; std::string curLocale = setlocale(LC_ALL, NULL); setlocale(LC_ALL, "chs"); const wchar_t* _source = ws.c_str(); size_t _dsize = 2 * ws.size() + 1; char* _dest = new char[_dsize]; memset(_dest, 0x0, _dsize); wcstombs_s(&i, _dest, _dsize, _source, _dsize); std::string result = _dest; delete[] _dest; setlocale(LC_ALL, curLocale.c_str()); return result; } std::wstring s2ws(const std::string &s) { size_t i; std::string curLocale = setlocale(LC_ALL, NULL); setlocale(LC_ALL, "chs"); const char* _source = s.c_str(); size_t _dsize = s.size() + 1; wchar_t* _dest = new wchar_t[_dsize]; wmemset(_dest, 0x0, _dsize); mbstowcs_s(&i, _dest, _dsize, _source, _dsize); std::wstring result = _dest; delete[] _dest; setlocale(LC_ALL, curLocale.c_str()); return result; }