Linux上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码。
一、利用iconv函数族进行编码转换
iconv函数族的头文件是iconv.h,使用前需包含之。
#include <iconv.h>
iconv函数族有三个函数,原型如下:
1) iconv_t iconv_open(const char *tocode, const char *fromcode);
此函数说明将要进行哪两种编码的转换,tocode是目标编码,fromcode是原编码,该函数返回一个转换句柄,供以下两个函数使用。
(2) size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);
此函数从inbuf中读取字符,转换后输出到outbuf中,inbytesleft用以记录还未转换的字符数,outbytesleft用以记录输出缓冲的剩余空间。
(3) int iconv_close(iconv_t cd);
此函数用于关闭转换句柄,释放资源。
例子1: 用C语言实现的转换示例程序
#include <iconv.h>
#include <stdio.h>
#define OUTLEN 255
/*代码转换:从一种编码转为另一种编码*/
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
{
iconv_t cd;
int rc;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
/*UNICODE码转为GB2312码*/
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
/*GB2312码转为UNICODE码*/
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}
void main()
{
char *in_utf8 = "姝e?ㄥ??瑁?";
char *in_gb2312 = "正在安装";
char out[OUTLEN];
int rc;
/*unicode码转为gb2312码*/
rc = u2g(in_utf8,strlen(in_utf8),out,OUTLEN);
printf("unicode-->gb2312 out=%sn",out);
//gb2312码转为unicode码
rc = g2u(in_gb2312,strlen(in_gb2312),out,OUTLEN);
printf("gb2312-->unicode out=%sn",out);
}
例子2: 用C++语言实现的转换示例程序
/* f.cpp : 代码转换示例C++程序 */
#include <iconv.h>
#include <iostream>
#define OUTLEN 255
using namespace std;
// 代码转换操作类
class CodeConverter {
private:
iconv_t cd;
public:
// 构造
CodeConverter(const char *from_charset,const char *to_charset) {
cd = iconv_open(to_charset,from_charset);
}
// 析构
~CodeConverter() {
iconv_close(cd);
}
// 转换输出
int convert(char *inbuf,int inlen,char *outbuf,int outlen) {
char **pin = &inbuf;
char **pout = &outbuf;
memset(outbuf,0,outlen);
return iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen);
}
};
int main(int argc, char **argv)
{
char *in_utf8 = "姝e?ㄥ??瑁?";
char *in_gb2312 = "正在安装";
char out[OUTLEN];
// utf-8-->gb2312
CodeConverter cc = CodeConverter("utf-8","gb2312");
cc.convert(in_utf8,strlen(in_utf8),out,OUTLEN);
cout << "utf-8-->gb2312 in=" << in_utf8 << ",out=" << out << endl;
// gb2312-->utf-8
CodeConverter cc2 = CodeConverter("gb2312","utf-8");
cc2.convert(in_gb2312,strlen(in_gb2312),out,OUTLEN);
cout << "gb2312-->utf-8 in=" << in_gb2312 << ",out=" << out << endl;
}
二、利用iconv命令进行编码转换
iconv命令用于转换指定文件的编码,默认输出到标准输出设备,亦可指定输出文件。
用法: iconv [选项...] [文件...]
有如下选项可用:
输入/输出格式规范:
-f, --from-code=名称 原始文本编码
-t, --to-code=名称 输出编码
信息:
-l, --list 列举所有已知的字符集
输出控制:
-c 从输出中忽略无效的字符
-o, --output=FILE 输出文件
-s, --silent 关闭警告
--verbose 打印进度信息
-?, --help 给出该系统求助列表
--usage 给出简要的用法信息
-V, --version 打印程序版本号
例子:
iconv -f utf-8 -t gb2312 aaa.txt >bbb.txt
这个命令读取aaa.txt文件,从utf-8编码转换为gb2312编码,其输出定向到bbb.txt文件。
概述
iconv是一个通过unicode作为中间码实现各种内码间相互转换的库,它基本上囊括了世界上所有编码方式,例如,ASCII、GB2312、 GBK、 GB18030、BIG5、UTF-8、UCS-2、UCS-2BE、UCS-2LE、UCS-4、UCS-4BE、UCS-4LE、UTF-16、 UTF-16BE、UTF-16LE、UTF-32、UTF-32BE、UTF-32LE、UTF-7等等等,除此之外,还包括泰语、日语、韩语、西欧等国家语言的编码。下面我们演示如何使用iconv实现Big5到GB2312的转换,当然只要简单修改一下便可实现iconv支持任何编码间的转换。
下载
libiconv是linux版本的iconv,可在 http://www.gnu.org/software/libiconv/ 下载
iconv的win32版本可以在 http://gnuwin32.sourceforge.net/packages/libiconv.htm 下载
SVN源码
另外,还有一些演示代码,需要的可以到我的SVN下载
http://xcyber.googlecode.com/svn/trunk/Convert/
演示代码
/****************************************************************************
* Big5ToGB2312 - Convert Big5 encoding file to GB2312 encoding file
* File:
* Big5ToGb2312.c
* Description:
* Convert Big5 encoding file to GB2312 encoding file using iconv library
* Author:
* XCyber email:XCyber@sohu.com
* Date:
* August 7, 2008
* Other:
* visit http://www.gnu.org/software/libiconv/ for more help of iconv
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <locale.h>
#include "../iconv-1.9.2.win32/include/iconv.h"
//#pragma comment(lib, "../iconv-1.9.2.win32/lib/iconv.lib") // using iconv dynamic-link lib, iconv.dll
#pragma comment(lib, "../iconv-1.9.2.win32/lib/iconv_a.lib") // using iconv static lib
#define BUFFER_SIZE 1024 //BUFFER_SIZE must >= 2
void usage()
{
printf("\nBig5ToGB2312 - Convert Big5 encoding file to GB2312 encoding file\n");
printf(" on August 7, 2008\n");
printf(" Usage:\n");
printf(" Big5ToGB2312 [Big5 file(in)] [GB2312 file(out)]\n\n");
}
int main(int argc, char* argv[])
{
FILE * pSrcFile = NULL;
FILE * pDstFile = NULL;
char szSrcBuf[BUFFER_SIZE];
char szDstBuf[BUFFER_SIZE];
size_t nSrc = 0;
size_t nDst = 0;
size_t nRead = 0;
size_t nRet = 0;
char *pSrcBuf = szSrcBuf;
char *pDstBuf = szDstBuf;
iconv_t icv;
int argument = 1;
//check input arguments
if(argc != 3)
{
usage();
return -1;
}
pSrcFile = fopen(argv[1],"r");
if(pSrcFile == NULL)
{
printf("can't open source file!\n");
return -1;
}
pDstFile = fopen(argv[2],"w");
if(pSrcFile == NULL)
{
printf("can't open destination file!\n");
return -1;
}
//initialize iconv routine, perform conversion from BIG5 to GB2312
//TODO: if you want to perfom other type of coversion, e.g. GB2312->BIG5, GB2312->UTF-8 ...
//just change following two paremeters of iconv_open()
icv = iconv_open("GB2312","BIG5");
if(icv == 0)
{
printf("can't initalize iconv routine!\n");
return -1;
}
//enable "illegal sequence discard and continue" feature, so that if met illeagal sequence,
//conversion will continue instead of being terminated
if(iconvctl (icv ,ICONV_SET_DISCARD_ILSEQ,&argument) != 0)
{
printf("can't enable \"illegal sequence discard and continue\" feature!\n");
return -1;
}
while(!feof(pSrcFile))
{
pSrcBuf = szSrcBuf;
pDstBuf = szDstBuf;
nDst = BUFFER_SIZE;
// read data from source file
nRead = fread(szSrcBuf + nSrc,sizeof(char),BUFFER_SIZE - nSrc,pSrcFile);
if(nRead == 0)
break;
// the amount of data to be converted should include previous left data and current read data
nSrc = nSrc + nRead;
//perform conversion
nRet = iconv(icv,(const char**)&pSrcBuf,&nSrc,&pDstBuf,&nDst);
if(nRet == -1)
{
// include all case of errno: E2BIG, EILSEQ, EINVAL
// E2BIG: There is not sufficient room at *outbuf.
// EILSEQ: An invalid multibyte sequence has been encountered in the input.
// EINVAL: An incomplete multibyte sequence has been encountered in the input
// move the left data to the head of szSrcBuf in other to link it with the next data block
memmove(szSrcBuf,pSrcBuf,nSrc);
}
pan> //wirte data to destination file
fwrite(szDstBuf,sizeof(char),BUFFER_SIZE - nDst,pDstFile);
}
iconv_close(icv);
fclose(pSrcFile);
fclose(pDstFile);
printf("conversion complete.\n");
return 0;
}