在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库。

iconv函数库有以下三个函数

1
2
3
4
5
6
#include <iconv.h>
iconv_t iconv_open(const char *tocode, const char *fromcode); //return (iconv_t)-1 if failed
size_t iconv(iconv_t cd,
char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft); //return (size_t)-1 if failed
int iconv_close(iconv_t cd); //return -1 if failed

这三个函数的功能显而易见,分别是打开一个iconv_t句柄,转换字符串以及关闭一个iconv_t句柄。其中有必要一说的是iconv函数,这个函数十分容易用错。

iconv函数的五个参数中,第一个参数是iconv句柄,第二、三个参数是需要转换的字符串的地址和长度的地址,第四、五个参数是存储结果的字符串的地址和长度的地址,注意这里传的都是地址,因为这四个参数的值都有会被iconv函数改变。iconv会逐步的将*inbuf中的字符转换到*outbuf中,并增加*inbuf指针减少*inbytesleft的值,以及增加*outbuf指针减少*outbytesleft的值。

iconv函数会因为以下四种原因停止并返回:

  1. *input中遇到了一个非法的多字节序列,返回(size_t)-1并置errno=EILSEQ,返回时*inbuf指向非法字符的开头。
  2. *input全部转换完,返回不可转换的字符数。
  3. *input中遇到了一个不完整的多字节序列,返回(size_t)-1并置errno=EINVAL,返回时*inbuf指向不完整字符的开头。
  4. *output空间不够,返回(size_t)-1并置errno=E2BIG。

以下给出一个示例函数,将一个字符串从utf-8转换成gbk后再重新转换成utf-8。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iconv.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h> int charset_convert(const char *from_charset, const char *to_charset,
char *in_buf, size_t in_left, char *out_buf, size_t out_left) {
iconv_t icd;
char *pin = in_buf;
char *pout = out_buf;
size_t out_len = out_left;
if ((iconv_t)-1 == (icd = iconv_open(to_charset,from_charset))) {
return -1;
}
if ((size_t)-1 == iconv(icd, &pin, &in_left, &pout, &out_left)) {
iconv_close(icd);
return -1;
}
out_buf[out_len - out_left] = 0;
iconv_close(icd);
return (int)out_len - out_left;
} int main(int argc, char *argv[]) {
char *from_str = "你好,中南。- Hello, CSU.";
char *to_str_gbk, *to_str_utf8;
int len;
//utf-8 => gbk
to_str_gbk = (char*)calloc(1, strlen(from_str) * 3);
if (-1 == (len = charset_convert("UTF-8", "GB2312", from_str,
strlen(from_str), to_str_gbk, strlen(from_str) * 3))) {
perror("UTF8=>GBK error");
}
//gbk => utf8
to_str_utf8 = (char*)calloc(1, len * 3);
if (-1 == (len = charset_convert("GB2312", "UTF-8", to_str_gbk,
len, to_str_utf8, len * 3))) {
perror("GBK=>UTF8 error");
}
//output
printf("original : %s\n", from_str);
printf("to gbk : %s\n", to_str_gbk);
printf("gbk to utf8: %s\n", to_str_utf8);
}

我用的xshell连接到虚拟机,先将terminal的编码设置为utf-8运行,结果如下

1
2
3
original   : 你好,中南。- Hello, CSU.
to gbk : ţºã¬אŏ¡£- Hello, CSU.
gbk to utf8: 你好,中南。- Hello, CSU.

再将terminal的编码设置为gbk运行,结果如下

1
2
3
original   : 浣犲ソ锛屼腑鍗椼€? Hello, CSU.
to gbk : 你好,中南。- Hello, CSU.
gbk to utf8: 浣犲ソ锛屼腑鍗椼€? Hello, CSU.

可见,在相应的编码下,对应的字符串能正常显示。

http://vimersu.win/blog/2014/03/04/linux-iconv/

在Linux下使用iconv转换字符串编码的更多相关文章

  1. windows下的文件到linux下乱码 iconv 修改文件编码

    conv [选项...] [文件...] 有如下选项可用: 输入/输出格式规范:-f, --from-code=名称 原始文本编码-t, --to-code=名称 输出编码 信息:-l, --list ...

  2. linux下改变文件的字符编码

    首先确定文件的原始字符编码: $ file -bi test.txt 然后用 iconv 转换字符编码 $ iconv -f from-encoding -t to-encoding file > ...

  3. linux下精确替换某个字符串

    1.linux下精确替换某个字符串 sed -i 's/\<old\>/new/g' filename.txt 2.举例: 2.1有个文件名为filename.txt,内容如下: newd ...

  4. 怎么将linux下的项目转换成windows的VS2010下的项目?

    怎么将linux下的项目转换成windows的VS2010下的项目?             不显示删除回复             显示所有回复             显示星级回复        ...

  5. Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty

    原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...

  6. linux iconv 转换文件编码

    查看文件编码file -i filename 递归转换(包括子文件夹)find default -type d -exec mkdir -p utf/{} \;find default -type f ...

  7. Linux下时间格式转换及获取方法

    Linux下使用clock_gettime给程序计时 #include <stdio.h> #include <unistd.h> #include <stdlib.h& ...

  8. 让linux下的eclipse支持GBK编码

    原文链接:http://leaze.blog.51cto.com/83088/195584 今天,把windows下的工程导入到了Linux下eclipse中,由于以前的工程代码,都是GBK编码的,而 ...

  9. Python3中转换字符串编码

    在使用subprocess调用Windows命令时,遇到了字符串不显示中文的问题,源码如下:#-*-coding:utf-8-*-__author__ = '$USER' #-*-coding:utf ...

随机推荐

  1. 剑指offer第五题

    输入一个链表,从尾到头打印链表每个节点的值.  但是 根据往常的经验 如果if里面有return了 就不要写else了  import java.util.ArrayList; import java ...

  2. js 和 jsp关系

    http://stackoverflow.com/questions/11718063/use-javascript-or-jquery-inside-a-cif-statement 纠结了半天的问题

  3. poj 1149 pigs ---- 最大流

    题意以及分析:http://ycool.com/post/zhhrrm6#rule3 主要是建图,简化图,然后在套最大流的模板. #include <iostream> #include& ...

  4. char str[] 与 char *str的区别详细解析

    char* get_str(void) { char str[] = {"abcd"}; return str; } char str[] = {"abcd"} ...

  5. BZOJ 1570: [JSOI2008]Blue Mary的旅行( 二分答案 + 最大流 )

    二分答案, 然后对于答案m, 把地点分成m层, 对于边(u, v), 第x层的u -> 第x+1层的v 连边. 然后第x层的u -> 第x+1层的u连边(+oo), S->第一层的1 ...

  6. Why Study JavaScript?

    JavaScript is one of the 3 languages all web developers must learn: 1. HTML to define the content of ...

  7. 一个简单的php函数调用实例

    需求分析: funcs.php (这个文件,我们定义了一个函数) <?php //我们一个计算,+ - * / 的代码集合->函数 //1. function 是一个关键字 //2. ji ...

  8. UIKit之浅析UIButton

    UIButton * button =[[UIButton alloc]init]; button.backgroundColor=[UIColor redColor]; [button setTit ...

  9. mysql binlog 混合模式 出现的基于sql的数据不一致,主要是now()这类函数导致

  10. 适配器模式—STL中的适配器模式分析

    适配器模式通常用于将一个类的接口转换为客户需要的另外一个接口,通过使用Adapter模式能够使得原本接口不兼容而不能一起工作的类可以一起工作. 这里将通过分析c++的标准模板库(STL)中的适配器来学 ...