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;
}