您的位置:首页 > 编程语言 > C语言/C++

C语言字符编码处理

2015-06-19 22:00 323 查看
一、字符编码识别

1、简介

uchardet是一个开源的用于文本编码检测的C语言库,其功能模块是用C++实现的,通过一定数量的字符样本独立的分析出文本的编码,当前已经支持UTF-8/GB13080/BIG5等共30多种编码。

参考:

https://www.byvoid.com/zhs/blog/encoding-autodetector-uchardet/


2、安装

https://github.com/BYVoid/uchardet http://www.filewatcher.com/m/uchardet_0.0.1.orig.tar.gz.179207-0.html[/code] 
3、实例

example1.c

#include <stdio.h>
#include <uchardet/uchardet.h>

/* 样本数量 */
#define NUMBER_OF_SAMPLES    (2048)

int main(int argc, char* argv[])
{
FILE* file;
char buf[NUMBER_OF_SAMPLES];
int len;
uchardet_t ud;

/* 打开被检测文本文件,并读取一定数量的样本字符 */
file = fopen("gb18030.txt", "rt");
len = fread(buf, sizeof(char), NUMBER_OF_SAMPLES, file);
fclose(file);

/* 通过样本字符分析文本编码 */
ud = uchardet_new();
if(uchardet_handle_data(ud, buf, len) != 0)    /* 如果样本字符不够,那么有可能导致分析失败 */
{
printf("分析编码失败!\n");
return -1;
}
uchardet_data_end(ud);
printf("文本的编码方式是%s。\n", uchardet_get_charset(ud));    /* 获取并打印文本编码 */
uchardet_delete(ud);

return 0;
}

编译

g++ -g -o example1 example1.c -I../src -L../src -static -luchardet

运行





二、字符编码转换

1、简介

由于历史原因,国际化的文字常常由于语言或者国家的原因使用不同的编码。libiconv库为需要做转换的应用提供了一个iconv()的函数,以实现一个字符编码到另一个字符编码的转换。

2、安装

http://www.gnu.org/software/libiconv/


3、API

iconv函数族有三个函数,原型如下:

iconv_t iconv_open(const char *tocode, const char *fromcode);

此函数说明将要进行哪两种编码的转换,tocode是目标编码,fromcode是原编码,该函数返回一个转换句柄,供以下两个函数使用。

size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);

此函数从inbuf中读取字符,转换后输出到outbuf中,inbytesleft用以记录还未转换的字符数,outbytesleft用以记录输出缓冲的剩余空间。

int iconv_close(iconv_t cd);

此函数用于关闭转换句柄,释放资源。

4、实例

参考:

http://www.linuxidc.com/Linux/2014-11/109066.htm http://www.cnblogs.com/lancidie/archive/2013/04/12/3016965.html[/code] 
example1.c

#include<stdio.h>                                                                                                                                        #include <string.h>
#include <iconv.h>

int ChangeCode( const char* pFromCode,
const char* pToCode,
const char* pInBuf,
size_t* iInLen,
char* pOutBuf,
size_t* iOutLen );

int main( int argc, char* argv[] )
{
char sInBuf[100];
char sOutBuf[100];
size_t iInLen = 0;
size_t iOutLen = 100;
int iRet;
strcpy( sInBuf, "测试 Test Source" );
puts(sInBuf);

memset( sOutBuf, 0x00, 100 );
iInLen = strlen( sInBuf );
iRet = ChangeCode( "GBK", "UTF-16", sInBuf, &iInLen, sOutBuf, &iOutLen );
puts(sOutBuf);
iRet = ChangeCode( "UTF-16", "GBK", sOutBuf, &iOutLen , sOutBuf, &iOutLen );
puts(sOutBuf);
return 0;
}

int ChangeCode( const char* pFromCode,
const char* pToCode,
const char* pInBuf,
size_t* iInLen,
char* pOutBuf,
size_t* iOutLen )
{
int iRet;
//打开字符集转换
iconv_t hIconv = iconv_open( pToCode, pFromCode );
if ( -1 == (int)hIconv )
{
return -1;//打开失败,可能不支持的字符集
}
//开始转换
iRet = iconv( hIconv, (const char**)(&pInBuf), iInLen, (char**)(&pOutBuf), iOutLen );
//关闭字符集转换
iconv_close( hIconv );
return iRet;
}

编译

gcc -g -o example1 example1.c  -liconv

运行



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: