ansi utf8与unicode互转函数
乱码伤不起啊。。。。
下面是转换代码。。。
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 | cc@insight-labs.org