免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1752 | 回复: 7

字符转换问题,大家帮忙看看 [复制链接]

论坛徽章:
0
发表于 2010-02-26 15:06 |显示全部楼层
一个简单的例子如下:
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <iconv.h>
  5. #include <stdlib.h>
  6. #define LENGTH 100
  7. int main()
  8. {
  9.     char *pin = "helloword你好";
  10.     //char pout[100];
  11.     char *pout = (char *)malloc(LENGTH);
  12.     iconv_t cd;
  13.     size_t inlen = 0,outlen = LENGTH;
  14.   char *p = pin;
  15.     char *p2 = pout;

  16.     memset(pout,0,LENGTH);
  17.     cd = iconv_open("gb2312","utf-8");
  18.     if(cd == (iconv_t)-1)
  19.         perror("iconv_open");
  20.     if(iconv(cd,&p,&inlen,&p2,&outlen) < 0)
  21.         perror("iconv");
  22.   iconv_close(cd);
  23.     printf("%s\n",pin);
  24.     printf("%s\n",pout);
  25.   return 0;
  26. }
复制代码
运行后不能输出转换后的值,即printf("%s\n",pout);这条语句没有打印出结果。不明白为什么,大家帮忙看看。谢谢了

论坛徽章:
323
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
发表于 2010-02-26 15:12 |显示全部楼层
inlen填源串的长度

论坛徽章:
0
发表于 2010-02-26 16:48 |显示全部楼层
谢谢二楼,是我疏忽大意了,但是我将inlen赋值为:strlen(pin)之后,“helloword你好”中的“你好”没有打印出来,打印出来的是这个东西:▒▒。怎么回事啊?

论坛徽章:
0
发表于 2010-02-26 21:04 |显示全部楼层
我记得 strlen 只能用来统计 ascii 吧……

论坛徽章:
0
发表于 2010-03-01 10:15 |显示全部楼层
我先统计汉字的个数,将字符总长度传给iconv函数,但是结果仍与预期的不符合啊,代码如下:
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <iconv.h>
  5. #include <stdlib.h>

  6. int main()
  7. {
  8.         char *pin = "hello好word你好";
  9.         char *pout = (char *)malloc(100);
  10.         iconv_t cd;
  11.         size_t inlen,outlen = 100;
  12.   char *p = pin;
  13.         char *p2 = pout;
  14.         int num = 0,num2 = 0;
  15.         size_t len = 0;
  16.         char *p3 = pin;
  17.         while(*p3)
  18.         {
  19.                 if((*p3) < 0)
  20.                         num++;
  21.                 else
  22.                         num2++;
  23.                 p3++;
  24.         }
  25.         inlen = num/3;
  26.         len = num2 + num;
  27.         printf("共有%d个汉字\n",inlen);
  28.         printf("字符串长度为%d\n",len);
  29.         memset(pout,0,100);
  30.         cd = iconv_open("gb2312","utf-8");
  31.         if(cd ==(iconv_t)-1)
  32.                 perror("iconv_open");
  33.         if(iconv(cd,&p,&len,&p2,&outlen) < 0)
  34.                 perror("iconv");
  35.         iconv_close(cd);
  36.         printf("%s\n",pin);
  37.         printf("%s\n",pout);
  38.   return 0;
  39. }
复制代码
输出结果为:
共有3个汉字
字符串长度为18
hello好word你好
hello▒▒word▒▒▒

论坛徽章:
0
发表于 2010-03-01 10:22 |显示全部楼层
我的操作系统为红帽5.2企业版

论坛徽章:
0
发表于 2010-03-01 10:39 |显示全部楼层
[quote]谢谢二楼,是我疏忽大意了,但是我将inlen赋值为:strlen(pin)之后,“helloword你好”中的“你好”没有打印 ...


    这个跟 你使用的terminal有关,你需要在terminal中设置显示的编码为gb2312,就可以看见"你好“了,但使用utf-8编码的串就显示不出来了

论坛徽章:
0
发表于 2010-03-03 08:51 |显示全部楼层
应该是你的编码方式不支持的原因
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP