亂碼傷不起啊。。。。
下面是轉換代碼。。。
inline wstring utf8ToUnicode(string str)
{
//UTF8 to Unicode
size_t wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), str.length(), NULL, 0);
//分配空間要給''留個空間,MultiByteToWideChar不會給''空間
wchar_t* wszString = new wchar_t[wcsLen + 1];
memset(wszString, 0, sizeof(wchar_t)*(wcsLen + 1));
//轉換
::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), str.length(), wszString, wcsLen);
wstring wstr= wszString;
delete wszString;
return wstr;
}
inline string unicode2utf8(wstring wstr)
{
int len; www.2cto.com
len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len*2 + 2];
memset(szUtf8, 0, len * 2 + 2);
WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)wstr.c_str(), -1, szUtf8, len, NULL,NULL);
string str= szUtf8;
delete szUtf8;
return str;
}
摘自 ccSec | [email protected]