char outRoleName[34] = {0};
ConvertCode("gbk", "UTF-8", pszGBK, outRoleName, 34);
bool ConvertCode(const char* fromcode, const char* tocode, const char* src, char* destbuf, size_t destbufsize)
{
//GBK to UTF8
wchar_t * lpUnicodeStr = NULL;
int nRetLen = 0;
if(!src) //如果GBK字符串为NULL则出错退出
{
return false;
}
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)src,-1,NULL,NULL); //获取转换到Unicode编码后所需要的字符空间长度
lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间
nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)src,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码
if(!nRetLen) //转换失败则出错退出
{
return false;
}
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,NULL); //获取转换到UTF8编码后所需要的字符空间长度
if(!destbuf) //输出缓冲区为空则返回转换后需要的空间大小
{
return false;
}
if(destbufsize < nRetLen) //如果输出缓冲区长度不够则退出
{
if(lpUnicodeStr)
{
delete []lpUnicodeStr;
}
return false;
}
nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)destbuf,destbufsize,NULL,NULL); //转换到UTF8编码
if(lpUnicodeStr)
delete []lpUnicodeStr;
return true;
}