您的位置:首页 > 其它

[置顶] GBK编码数据转码为UTF-8格式

2017-04-06 17:20 337 查看
#include <windows.h>


#include <wchar.h>

int CFileListModel::GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen)

{

wchar_t * lpUnicodeStr = NULL;

int nRetLen = 0;


if(!lpGBKStr)  //如果GBK字符串为NULL则出错退出

return 0;


nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,NULL);  //获取转换到Unicode编码后所需要的字符空间长度

lpUnicodeStr = new WCHAR[nRetLen + 1];  //为Unicode字符串空间

nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen);  //转换到Unicode编码

if(!nRetLen)  //转换失败则出错退出

return 0;


nRetLen = ::WideCharToMultiByte(C
c3a5
P_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,NULL);  //获取转换到UTF8编码后所需要的字符空间长度


if(!lpUTF8Str)  //输出缓冲区为空则返回转换后需要的空间大小

{

if(lpUnicodeStr)

delete []lpUnicodeStr;

return nRetLen;

}


if(nUTF8StrLen < nRetLen)  //如果输出缓冲区长度不够则退出

{

if(lpUnicodeStr)

delete []lpUnicodeStr;

return 0;

}


nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL);  //转换到UTF8编码


if(lpUnicodeStr)

delete []lpUnicodeStr;


return nRetLen;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: