Linux下实现UTF-8和GB2312互相转换的方法
实例如下:
#include<iconv.h>#include<stdio.h>
#include<iconv.h>
usingnamespacestd;
intutf8togb2312(constchar*sourcebuf,size_tsourcelen,char*destbuf,size_tdestlen){
iconv_tcd;
if((cd=iconv_open("gb2312","utf-8"))==0)
return-1;
memset(destbuf,0,destlen);
constchar**source=&sourcebuf;
char**dest=&destbuf;
if(-1==iconv(cd,source,&sourcelen,dest,&destlen))
return-1;
iconv_close(cd);
return0;
}
intgb2312toutf8(constchar*sourcebuf,size_tsourcelen,char*destbuf,size_tdestlen){
iconv_tcd;if((cd=iconv_open("utf-8","gb2312"))==0)
return-1;memset(destbuf,0,destlen);
constchar**source=&sourcebuf;
char**dest=&destbuf;
if(-1==iconv(cd,source,&sourcelen,dest,&destlen))
return-1;
iconv_close(cd);
return0;
}
intmain(){
charline[80];
chardest_gb2312[200];
chardest_utf8[200];
intret;
FILE*fp=fopen("love.txt","r");
if(fp==NULL)
{
printf("ohmydear,openfail...\n");
return1;
}
memset(line,0,80);
if(NULL==fgets(line,80,fp))
{
printf("ohfgetsfail...\n");
return2;
}
printf("-----:%s\n",line);
ret=gb2312toutf8(line,strlen(line),dest_gb2312,200);
//ret=gb2312toutf8(dest_gb2312,strlen(dest_gb2312),dest_utf8,80);
printf("ret=%d\n\n\n",ret);
printf("gb2312:%s\n",line);
printf("utf-8:%s\n\n\n\n\n\n",dest_gb2312);
ret=utf8togb2312(dest_gb2312,strlen(dest_gb2312),dest_utf8,200);
printf("ret=%d\n\n\n",ret);
printf("utf-8:%s\n",dest_gb2312);
printf("gb2312:%s\n",dest_utf8);
return0;
}
以上就是小编为大家带来的Linux下实现UTF-8和GB2312互相转换的方法全部内容了,希望大家多多支持毛票票~
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下