Linux C语言使用iconv报错

Python014

Linux C语言使用iconv报错,第1张

utf-8转换为gb18030时iconv返回报错 Invalid or incomplete multibyte or wide character

还以为是指针什么的没用对,没想到是CPP文件的编码问题

首先是在本地创建的CPP文件,默认是UTF-8编码,后来转换成Ansi编码

再次上传编译后,这个问题就解决了。

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