紧急,一个编码转换的问题!!utf-8编码转成GBK,因为位数不同而产生字符丢掉问题

liufx 2008-08-29 10:15:19
紧急,一个编码转换的问题!!utf-8编码转成GBK,因为位数不同而产生字符丢掉问题.如果汉字个数 为奇数时就丢失,偶数没有这个问题。请大家帮忙解决?


我先将宽字符串转换成utf8编码的多字节字符串(ConvertGBKToUtf8),然后再转换成unicode编码字符串(ANSI2Unicode()--〉在此出现了字符丢失现象)。

int ConvertGBKToUtf8(const wchar_t * gbk_str, int len, char ** utf8_str)
{

len = WideCharToMultiByte(CP_UTF8, 0,
(LPCWSTR)gbk_str, -1,
NULL,
0, NULL, NULL);

*utf8_str = new char[len + 1];
if(*utf8_str == NULL)
return -1;

memset(*utf8_str, 0, len + 1);

len = WideCharToMultiByte (CP_UTF8, 0, (LPCWSTR)gbk_str,
-1, *utf8_str, len, NULL,NULL);

return len;
}

int ANSIToUnicode(LPCSTR lpMultiByteStr,int cbMultiByte, wchar_t **UnicodeBuffer)
{
*UnicodeBuffer = NULL;

int iLen=MultiByteToWideChar (CP_ACP, 0, lpMultiByteStr, cbMultiByte, NULL,0);

if(iLen <= 0)
return 0;

*UnicodeBuffer = new wchar_t[iLen + 1];
if(*UnicodeBuffer == NULL)
return -1;

memset(*UnicodeBuffer, 0, sizeof(wchar_t) * (iLen + 1));

iLen = MultiByteToWideChar (CP_ACP, 0,
lpMultiByteStr, cbMultiByte,
*UnicodeBuffer, iLen);

return iLen;
}
...全文
97 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
第二个函数CP_ACP->CP_UTF8

char *p = NULL;
ConvertGBKToUtf8(L"你好啊", -1, &p);

wchar_t* q = NULL;
ANSIToUnicode(p, -1, &q);
测试没问题
转了一圈而已


第二个输入字符既然已经是UTF-8,为什么要用CP_ACP呢??

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧