首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

字符集变换(iconv)出错求教

2012-07-04 
字符集转换(iconv)出错求教C/C++ code#include unistd.h#include stdio.h#include sys/types.h#incl

字符集转换(iconv)出错求教


C/C++ code
#include <unistd.h>#include <stdio.h>#include <sys/types.h>#include <iconv.h> #include <sys/stat.h>#include <fcntl.h>#define S 2000void convert(const char *fromset,const char *toset,char *from,int from_len,char *to,int to_len){ printf("%s is to be converted!\n",from); iconv_t cd,cdd; cd=iconv_open(toset,fromset); char **from2=&from; char **to2=&to; if(iconv(cd,from2,&from_len,to2,&to_len)==-1)  printf("Convert fail!\n"); else  printf("Convert success!\n"); iconv_close(cd); return ;}int main(){ char from[]="橞"; /*如果换成“你好”就可以成功?*/ char to[S]; convert("GB2312","UTF8",from,strlen(from),to,S);  //把gb2312转换成utf8 printf("%s\n",to); return 0;}


貌似遇到有些生僻字用那函数转换会出错,哪位朋友遇到过?紧急求教!

[解决办法]
汗, utf8源码文件里里写一个"橞", 你知道是哪3个字节吗?

C/C++ code
[User:root Time:21:11:31 Path:/home/liangdong/c]$ od -t x1 data.txt 0000000 e6 a9 9e 0a0000004[User:root Time:21:11:37 Path:/home/liangdong/c]$ cat data.txt 橞
[解决办法]
最关键的是"橞"根本就不是GB2312, 改成GB18030试试。
[解决办法]
你用 locale 看看本地设置是什么,一般来说都是 UTF-8,所以:

char from[]="橞" ;

貌似本来 from 就是 UTF-8 字符,如果按 gb2312 来转应该就会报错吧


[解决办法]
探讨

汗, utf8源码文件里里写一个"橞", 你知道是哪3个字节吗?

C/C++ code
[User:root Time:21:11:31 Path:/home/liangdong/c]$ od -t x1 data.txt
0000000 e6 a9 9e 0a
0000004
[User:root Time:21:11:37 Path:/home/liangdong/c]$ cat ……

[解决办法]
你的“橞”字是写在源代码文件里的,所以也得注意你的这个源代码文件的编码格式!
[解决办法]
同意3楼的,gb2312表示的汉字很少,试试 GBK 或 gb18030

热点排行