为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

GCT1015 2023-01-02 18:14:29
#include <unistd.h>#include <stdio.h>#include <sys/types.h>#include <iconv.h> //convert function#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");elseprintf("Convert success!\n");iconv_close(cd);return ;}int main(){char from[]="你好";  char to[S];convert("GB2312","BIG5",from,strlen(from),to,S); //把gb2312转换成big5printf("%s\n",to);return 0;}如果我把convert函数的参数“GB2312”和“BIG5”换成“UTF-8”和“GB2312”就可以成功,不知道为什么。谢谢了。
查看完整描述

2 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

显然GBK无法转换成BIG5。iconv的含义是将一个抽象的符号的编码进行转换。
但是如果一个符号比如“个”,可能在BIG5的编码中不存在(繁体字中不同)
GBK包含的是简体字,BIG5包含的是繁体字,Unicode包含全部,
所以
GBK->Unicode,Big5-Unicode (总是OK)
Unicode->GBK (当里面仅包含英文及简体时OK)
Unicode->BIG5 (当里面仅包含英文及繁体时OK)
GBK->Big5 (基本上不行,除非某些字没有特别的简体字)

GBK->Big5是汉字的简繁转换,不是编码转换,简体字转繁体字还有一个问题,一个简体字可能是对应多个繁体字,这种很难转换正确。繁体字转换成简体字相对难度低。 

说白了,即使意思一样,简体和繁体也是两个字,不能划等号。应当使用简繁通。


查看完整回答
反对 回复 2023-01-06
?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

1、iconv的含义是将一个抽象的符号的编码进行转换。
但是如果一个符号比如“个”,可能在BIG5的编码中不存在(繁体字中不同)
GBK包含的是简体字,BIG5包含的是繁体字,Unicode包含全部,
所以
GBK->Unicode,Big5-Unicode (总是OK)
Unicode->GBK (当里面仅包含英文及简体时OK)
Unicode->BIG5 (当里面仅包含英文及繁体时OK)
GBK->Big5 (基本上不行,除非某些字没有特别的简体字)
GBK->Big5是汉字的简繁转换,不是编码转换,简体字转繁体字还有一个问题,一个简体字可能是对应多个繁体字,这种很难转换正确。繁体字转换成简体字相对难度低。 

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 converted
output bytes

 


查看完整回答
反对 回复 2023-01-06
  • 2 回答
  • 0 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信