LONG UTF8ToGBK(const void * lpUTF8Str, string & str)
{
if(lpUTF8Str == NULL) return -1;
int nRetLen = 0;
//获取转换到Unicode编码后所需要的字符空间长度
nRetLen = ::MultiByteToWideChar(CP_UTF8, 0,
(char *)lpUTF8Str, -1, NULL, NULL);
WCHAR *lpUnicodeStr = new WCHAR[nRetLen + 1];
//为Unicode字符串空间
//转换到Unicode编码
nRetLen = ::MultiByteToWideChar(CP_UTF8, 0,
(char *)lpUTF8Str, -1, lpUnicodeStr, nRetLen);
if(!nRetLen)
{
delete []lpUnicodeStr; return -1;
}
//获取转换到GBK编码后所需要的字符空间长度
nRetLen = ::WideCharToMultiByte(CP_ACP, 0, lpUnicodeStr,
-1, NULL, NULL, NULL, NULL);
CHAR *lpGBKStr = new CHAR[nRetLen + 1];
nRetLen = ::WideCharToMultiByte(CP_ACP, 0, lpUnicodeStr,
-1, (char *)lpGBKStr, nRetLen, NULL, NULL);
//转换到GBK编码
if(!nRetLen)
{
delete []lpUnicodeStr;
delete []lpGBKStr;
return -2;
}
str = lpGBKStr;
delete []lpUnicodeStr;
delete []lpGBKStr;
return 0;
}