ut-8f转unicode
wstring UTF8ToUnicode(const string&str)
{
int len =0;
len = str.length();
int unicodeLen = MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);
wchar_t *pUnicode = new wchar_t[unicodeLen+1];
memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,(WCHAR*)pUnicode,unicodeLen);
wstring rt;
rt = (wchar_t*)pUnicode;
delete []pUnicode;
return rt;
}
unicode转UTF-8
string UnicodeToUTF(const wstring&str)
{
char* pElementText;
int len =0;
len = WideCharToMultiByte(CP_UTF8,0,str.c_str(),-1,NULL,0,NULL,NULL);
pElementText = new char[len+1];
memset(pElementText,0,(len+1)*sizeof(char));
WideCharToMultiByte(CP_UTF8,0,str.c_str(),-1,pElementText,len,NULL,NULL);
string strText;
strText = pElementText;
delete[] pElementText;
return strText;
}
//跨平台
string ws2s(const wstring ws) { string curLocale = setlocale(LC_ALL,NULL); setlocale(LC_ALL,"chs"); const wchar_t* Sourse = ws.c_str(); size_t dSize = ws.size() * 2 + 1; char* dest = new char[dSize]; memset(dest,0,dSize); wcstombs(dest,Sourse,dSize); string result = dest; delete[] dest; setlocale(LC_ALL,curLocale.c_str()); return result; } wstring s2ws(const string&s) { 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]; memset(dest,0,dSize); mbstowcs(dest,source,dSize); wstring result = dest; delete[] dest; setlocale(LC_ALL,curLocale.c_str()); return result; }