免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4571 | 回复: 6
打印 上一主题 下一主题

[应用] iconv字符编码转换问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-04-25 10:46 |只看该作者 |倒序浏览
一测试程序如下:
#include <iconv.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
        iconv_t cd;
        int rc;
        char **pin = &inbuf;
        char **pout = &outbuf;

        cd = iconv_open(to_charset,from_charset);
        if (cd==0){
                            printf("iconv_open\n";
                return -1;
        }
        memset(outbuf,0,outlen);
       
        if (iconv(cd,pin,&inlen,pout,&outlen) == -1)
        {
                        perror("iconv:\n";
                                iconv_close(cd);
                return -1;
        }
        iconv_close(cd);
               
                printf("inlen = %d outlen = %d\n",inlen,outlen);
        return 0;
}

int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
        return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}

int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
      //return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
      //return code_convert("gb18030","utf-8",inbuf,inlen,outbuf,outlen);
          return code_convert("gbk","utf-8",inbuf,inlen,outbuf,outlen);
}

int main()
{
    char GBK[64]="./你好";
    char out[128];
   
    memset(out,0x00,sizeof(out));
   
    g2u(GBK,14,out,12;
   
    printf("GBK=%s \n  utf=%s \n",GBK,out);

         if(mkdir(out,   0755)==-1)  
     {   
                      printf("mkdir   error";   
                      return   -1;   
     }  
   
    return 0;
   
   
   
    }
问题是:在我虚拟机上测试通过,生成一个叫 你好  的目录,但在我板子上运行结果确是(编译通过了)
iconv:
: Bad file descriptor
GBK=./▒▒▒
  utf=

论坛徽章:
0
2 [报告]
发表于 2014-04-25 10:47 |只看该作者
自己先顶一个

论坛徽章:
0
3 [报告]
发表于 2014-04-25 10:49 |只看该作者
会不会是我板子是linux 2.6.32 版本太低了

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
4 [报告]
发表于 2014-04-27 10:38 |只看该作者
开发板上的icov有问题,转换出错。

论坛徽章:
0
5 [报告]
发表于 2014-04-28 10:56 |只看该作者
具体怎么说 回复 4# Tinnal


   

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
6 [报告]
发表于 2014-04-28 23:12 |只看该作者
本帖最后由 Tinnal 于 2014-04-28 23:14 编辑

回复 5# 我的奋斗zcy

从你的程序来看,是因为ICONV函数接受的描述符错误导致的(Bad file descriptor), 回顾你的打开函数,你的错误判断是不对的。iconv_open的返回就看man手册的说明:

RETURN VALUE       
The iconv_open function returns a freshly allocated conversion descriptor. In case of error, it sets errno and returns (iconv_t)(−1).

因此,猜测你当前的iconv_open肯定的返回-1,然后你把这个-1传给iconv了,那当然就“Bad file descriptor”了。

回到iconv_open,出错的唯一可能也要看man手册:
ERRORS
The following error can occur, among others:
EINVAL
The conversion from fromcode to tocode is not supported by the implementation.


看来只有一种可能,就是你当前的iconv库不支持参数所描述的编码。而iconv所支持的编码是在编译iconv时选定的。因此,得出我上一个回贴的结论。

man手册提供了一个校验你的环境是否支持该编码转换的方法:
The values permitted for fromcode and tocode and the supported
combinations are system-dependent.  For the GNU C library, the
permitted values are listed by the iconv --list command, and all
combinations of the listed values are supported.  

也就是说如果你的开发板有iconv这个命令,输出iconv --list就能看到当前iconv库所支持的所有编码。iconv命令是和iconv库一同从iconv源吗编译出来的。


同时也提醒你,在Linux下编程最好的参考就是最直接的man手册。
以上内容拷贝至网页版本man手册:http://man7.org/linux/man-pages/man3/iconv_open.3.html
在Linux下输入 man iconv_open就行。




   

论坛徽章:
0
7 [报告]
发表于 2014-05-08 20:50 |只看该作者
前辈还是同样的程序,我后来自己移植了一个libiconv库,不报错了,但是结果还是
utf=空,后来我用echo $LANG看我板子的字体集是zh_CN,一般系统的会是zh_CN.UTF-8  是不是就是这个问题啊。
回复 1# 我的奋斗zcy


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP