转载来自:https://www.cnblogs.com/coolbear/archive/2013/05/24/3096683.html
using namespace std; 1、字符串中不含有汉字 |--string-->wstring string s("abc"); wstring ws(s.begin(), s.end());//或者wstring ws; ws.assign(s.begin(), s.end()); |--wstring-->string wstring ws(L"ABC"); string s(ws.begin(), ws.end());//或者string s; s.assign(ws.begin(), ws.end()); 2、字符串中含有汉字 |--Windows平台:(sizeof(wchar_t)=2) wstring myStringToWString(string s){ size_t convertedChars=0;//被转换的字符个数 wchar_t wcstring[128]; setlocale(LC_CTYPE,"chs");//处理汉字 mbstowcs_s(&convertedChars, wcstring, 128, s.c_str(), 128); return wstring(wcstring); } string myWStringToString(wstring ws){ size_t convertedChars=0;//被转换的字符个数 wchar_t cstring[128]; setlocale(LC_CTYPE,"chs");//处理汉字 wcstombs_s(&convertedChars, cstring, 128, ws.c_str(), 128); return string(cstring); } |--Linux平台(需要注意的是linux下sizeof(wchar_t)=4) wstring myStringToWString(string s){ wchar_t wcstring[128]; setlocale(LC_CTYPE,"chs");//处理汉字 mbstowcs(wcstring, s.c_str(), 128); return wstring(wcstring); } string myWStringToString(wstring ws){ wchar_t cstring[128]; setlocale(LC_CTYPE,"chs");//处理汉字 wcstombs(cstring, ws.c_str(), 128); return string(cstring); }
标签:string,cstring,wstring,wcstring,ws,128 From: https://www.cnblogs.com/8335IT/p/16715130.html