双字节字符与单字节字符转换
1 std::string wstring2string(const std::wstring& wstr) 2 { 3 int bufLen = WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),-1,NULL,0,NULL,NULL); 4 char* strbuf = new char[bufLen]; 5 WideCharToMultiByte(CP_UTF8,0,wstr.c_str(),-1,strbuf,bufLen,NULL,NULL); 6 std::string temp(strbuf); 7 delete[] strbuf; 8 9 return temp; 10 } 11 12 std::wstring string2wstring(const std::string& str) 13 { 14 int bufLen = MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0); 15 wchar_t* wstrbuf = new wchar_t[bufLen]; 16 MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,wstrbuf,bufLen); 17 std::wstring temp(wstrbuf); 18 delete[] wstrbuf; 19 20 return temp; 21 }
注意这里使用的是CP_UTF8 而不是 CP_ACP