求一个linux下c语言的方法

lanting918 2005-03-17 05:40:19
主要功能将一个中文字符串转换为ucs2编码
如“中国电子技术信息网”转换后为4E2D56FD75355B506280672F4FE1606F7F51

急啊
...全文
162 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wtogether 2005-03-18
  • 打赏
  • 举报
回复
sorry,看错了,用这个
int CharToUnicode(const char *cstr, char *ustr, int ul)
{
int cl = xstrlen(cstr);
iconv_t cd;
memset(ustr, 0x0, ul);
if ((cd=iconv_open("UTF-16BE", "GB2312"))==(iconv_t)-1)
{
snprintf(ustr, ul, "%s", cstr);
return(-1);
}
if (iconv(cd, &cstr, &cl, &ustr, &ul)==(size_t)-1)
{
snprintf(ustr, ul, "%s", cstr);
iconv_close(cd);
return(1);
}
iconv_close(cd);
return(0);
}
wtogether 2005-03-18
  • 打赏
  • 举报
回复
wchar_t *CharToUnicode(const char *cstr)
{
unsigned ml;
wchar_t* buf;
ml = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cstr, -1, NULL, 0);
buf = (wchar_t *)malloc(ml * sizeof(wchar_t));
if (buf == NULL) return(buf);
memset(buf, 0x0, ml * sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cstr, -1, buf, ml);
return(buf);
}

int main(int argc, char *argv[])
{
//4E2D56FD75355B506280672F4FE1606F7F51
unsigned char *p = "中国电子技术信息网";
wchar_t *pp = CharToUnicode(p);
while (*pp)
{
printf("%X", *pp);
pp++;
}
return 0;
}
cym64039 2005-03-18
  • 打赏
  • 举报
回复
把UTF-16BE改为unicode就可以了
lanting918 2005-03-18
  • 打赏
  • 举报
回复
to: wtogether
上面这个程序是把utf-16的转到gb2312吧?
lanting918 2005-03-18
  • 打赏
  • 举报
回复
谢谢wtogether
冷月清晖 2005-03-17
  • 打赏
  • 举报
回复
看看有没帮助:
http://www.linuxeden.com/edu/doctext.php?docid=2281
hofer_song 2005-03-17
  • 打赏
  • 举报
回复
2
哈哈
下班了
楼主没到Linux专区问问?
zhousqy 2005-03-17
  • 打赏
  • 举报
回复
up,沙發。

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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