#include <string>
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = * ws.size() + ;
char *_Dest = new char[_Dsize];
memset(_Dest,,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
std::string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
} std::wstring s2ws(const std::string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + ;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, , _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}
BOOL StringToWString(const std::string &str,std::wstring &wstr)
{
int nLen = (int)str.length();
wstr.resize(nLen,L' '); int nResult = MultiByteToWideChar(CP_ACP,,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen); if (nResult == )
{
return FALSE;
} return TRUE;
}
//wstring高字节不为0,返回FALSE
BOOL WStringToString(const std::wstring &wstr,std::string &str)
{
int nLen = (int)wstr.length();
str.resize(nLen,' '); int nResult = WideCharToMultiByte(CP_ACP,,(LPCWSTR)wstr.c_str(),nLen,(LPSTR)str.c_str(),nLen,NULL,NULL); if (nResult == )
{
return FALSE;
} return TRUE;
}
std::wstring StringToWString(const std::string &str)
{
std::wstring wstr(str.length(),L' ');
std::copy(str.begin(), str.end(), wstr.begin());
return wstr;
} //只拷贝低字节至string中
std::string WStringToString(const std::wstring &wstr)
{
std::string str(wstr.length(), ' ');
std::copy(wstr.begin(), wstr.end(), str.begin());
return str;
}