请原谅我发代码,我也知道发代码是可耻的,但是实在是不知道怎么回事了。

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <iconv.h> //convert function
#include <sys/stat.h>
#include <fcntl.h>

#define S 2000

void 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","BIG5",from,strlen(from),to,S); //把gb2312转换成big5
printf("%s\n",to);
return 0;
}

如果我把convert函数的参数“GB2312”和“BIG5”换成“UTF-8”和“GB2312”就可以成功,不知道为什么。
谢谢了。

GCT1015
浏览 90回答 2
2回答

慕的地8271018

显然GBK无法转换成BIG5。iconv的含义是将一个抽象的符号的编码进行转换。但是如果一个符号比如“个”,可能在BIG5的编码中不存在(繁体字中不同)GBK包含的是简体字,BIG5包含的是繁体字,Unicode包含全部,所以GBK->Unicode,Big5-Unicode (总是OK)Unicode->GBK (当里面仅包含英文及简体时OK)Unicode->BIG5 (当里面仅包含英文及繁体时OK)GBK->Big5 (基本上不行,除非某些字没有特别的简体字)GBK->Big5是汉字的简繁转换,不是编码转换,简体字转繁体字还有一个问题,一个简体字可能是对应多个繁体字,这种很难转换正确。繁体字转换成简体字相对难度低。&nbsp;说白了,即使意思一样,简体和繁体也是两个字,不能划等号。应当使用简繁通。

慕无忌1623718

1、iconv的含义是将一个抽象的符号的编码进行转换。但是如果一个符号比如“个”,可能在BIG5的编码中不存在(繁体字中不同)GBK包含的是简体字,BIG5包含的是繁体字,Unicode包含全部,所以GBK->Unicode,Big5-Unicode (总是OK)Unicode->GBK (当里面仅包含英文及简体时OK)Unicode->BIG5 (当里面仅包含英文及繁体时OK)GBK->Big5 (基本上不行,除非某些字没有特别的简体字)GBK->Big5是汉字的简繁转换,不是编码转换,简体字转繁体字还有一个问题,一个简体字可能是对应多个繁体字,这种很难转换正确。繁体字转换成简体字相对难度低。&nbsp;2、#include <iconv.h>size_t iconv(iconv_t cd,char **inbuf, size_t *inbytesleft,char **outbuf, size_t *outbytesleft);函数原型, outbuf是一个 char **类型在函数手册中:The iconv() function converts one multibyte character at a time, and for each character conversion it increments *inbuf and decrements*inbytesleft by the number of converted input bytes, it increments *outbuf and decrements *outbytesleft by the number of convertedoutput bytes&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP