字符集转换(iconv)出错求教
#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;}[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 来转应该就会报错吧
[解决办法]