ICONV下载
作者:互联网
ICONV下载
https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
#pc
$ ./configure CC=arm-none-linux-gnueabi-gcc
#NT
$./configure --host=arm-linux CC=arm-ca53-linux-gnueabihf-gcc
$make
编译命令
gcc -o iconv iconv.c -L ./lib -liconv
在./lib/.libs下取出so动态库文件
静态库
./configure --enable-shared=no --enable-static=yes
头文件说明
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);
此函数用于关闭转换句柄,释放资源。
编写demo实现字符编码转换
#include <iconv.h>
#include <stdio.h>
#include <string.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)
{
printf("open file failed!!!\n");
return -1;
}
memset(outbuf,0,outlen);
if (iconv(cd,pin,&inlen,pout,&outlen)==-1)
{
printf("convert failed!!!\n");
iconv_close(cd);
return -1;
}
iconv_close(cd);
return 0;
}
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
}
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}
int main(int argc, char *argv[])
{
char *in_utf8 = "æ£å?¨å??è£?";
char *in_gb2312 = "ÕýÔÚ°²×°";
char out[OUTLEN];
int rc;
if ((argc < 2) || (0 == strlen(argv[1])))
{
printf("\nInvaild input! incov file\n");
return 0;
}
rc = g2u(argv[1],strlen(argv[1]),out,OUTLEN);
printf("gb2312-->unicode out=%s\n",out);
return 1;
}
使用:
iconv -f gbk -t utf-8 -o outfile infile
iconv -f gbk -t utf-8 index.html > aautf8.html
convmv -f GBK -t UTF-8 *.mp3
convmv -f GBK -t UTF-8 --notest *.mp3
标签:ICONV,outbuf,int,iconv,cd,char,下载,inbuf 来源: https://blog.csdn.net/sunshineywz/article/details/100783761