博客搬家,原地址:https://langzi989.github.io/2017/07/17/C中编码问题/
编码介绍
在代码中我们通常不可避免的出现一些中文,这个时候我们就要考虑到中文的编码格式,如果不注意可能会导致乱码或者信息失真等问题。我们常用的中文编码有GBK,gb2312,Unicode等等。具体详细的介绍看下面几篇文章:
- http://www.cnblogs.com/lizhenghn/p/3690406.html
- http://docs.linuxtone.org/ebooks/C&CPP/c/apas03.html
C语言编码转换
在C语言中,如果需要讲编码进行转换,可以使用iconv系列函数。
头文件以及常用函数:
#include <iconv.h>
typedef void* iconv_t;
extern iconv_t iconv_open(const char* to_code, const char* from_code);
extern size_t iconv(iconv_t cd, char** restrict inbuf, size_t* in_left_buf, char** restrict outbuf, size_t* out_left_buf);
extern int iconv_close(iconv_t cd);
iconv_open
函数说明
此函数说明将要进行哪两种编码的转换,并返回一个转化句柄。
参数说明
- tocode:目标编码
- fromcode : 原编码
iconv
extern size_t iconv(iconv_t cd, char** restrict inbuf, size_t* in_left_buf, char** restrict outbuf, size_t* out_left_buf