学习使用libiconv库
libiconv库是一个基于GNU协议的开源库,主要是解决多语言编码处理转换等应用问题。
几个函数原型:
iconv_t iconv_open(const char *tocode, const char *fromcode);葛底斯堡战役
size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
int iconv_clo(iconv_t cd);
其中:
1. iconv_open是打开一个编码流,类似于打开一个编码管道(通道),出错则返回 -1;
2. iconv用于具体输入的转换,如果出错,则返回 -1,否则返回 0;
3. iconv_clo是关闭该管道(通道)。
medication
例子一要点:
1. iconv传递有5个参数;
2. 第3个参数和第5个参数一般是input和output实际分配的大小,一般是 sizeof(type)*strlen(string);
3. 第4个参数是不能直接传递指针的地址,因为iconv函数能够改变指针的值,所以需要复制一份指针变量;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iconv.h>
#define OUTLEN 255
int covert(char *, char *, char *, size_t , char *, size_t );
int main(int argc, char *argv[])
{
char *input = "中国";
size_t len = strlen(input);
char *output = (char *)malloc(OUTLEN);
covert("UTF-8", "GBK", input, len, output, OUTLEN);
printf("%s\n", output);
return 0;
}
int covert(char *desc, char *src, char *input, size_t ilen, char *output, size_t olen)
{
char **pin = &input;
char **pout = &output;
iconv_t cd = iconv_open(desc, src);
if (cd == (iconv_t)-1)
{
return -1;
}
memt(output, 0, olen);
if (iconv(cd, pin, &ilen, pout, &olen))
return -1;
iconv_clo(cd);
return 0;
}
例子二要点:
1. 对于大量需要转换的编码,上述函数covert不适合该方式
1. 内存的限制不能一次调用
2. 若分多次调用会频繁打开一个编码管道(通道),导致资源浪费
2. 最好的办法还是拆开该函数根据情况使用。
//translateSP.h:
#ifndef __TRANSLATESP_H_
#define __TRANSLATESP_H_
#include <iconv.h>
class TranslateSP
{
private:
iconv_t i_cd;
public:
size_t translate(char *src, size_t srcLen, char *desc, size_t descLen);
size_t convert(const char *from_chart, const char *to_chart,
mia farrow char *src, size_t srcLen, char *desc, size_t descLen);
public:
ranslateSP():i_cd(0){}
TranslateSP(const char *from_chart,const char *to_chart)
{
i_cd = iconv_open(to_chart, from_chart);
if ((iconv_t)-1 == i_cd)
printf("iconv open error!\n");
}
~TranslateSP()
{
if (i_cd)
iconv_clo(i_cd);
}
};
#endif
//translateSP.cpp:
remember的用法 pearl是什么意思
#include "translateSP.h"
#define MAX_LEN 200
size_t TranslateSP::translate(char *src, size_t srcLen, char *desc, size_t descLen)
{
che
char **inbuf = &src;
char **outbuf = &desc;
memt(desc, 0, descLen);
return iconv(i_cd, inbuf, &srcLe
n, outbuf, &descLen);
}
我的梦想
size_t TranslateSP::convert(const char *from_chart, const char *to_chart,
char *src, size_t srcLen, char *desc, size_t descLen)
{
char **inbuf = &src;
char **outbuf = &desc;
iconv_t cd = iconv_open(to_chart, from_chart);
if ((iconv_t)-1 == cd)
return (size_t)-1;
memt(desc, 0, descLen);
size_t n = iconv(cd, inbuf, &srcLen, outbuf, &descLen);
iconv_clo(cd);
return n;
}
int main(int argc, char *argv[])
{
char *str = "我爱zhong国! %#@#";
char *str1 = "i大量需要转换的编码";
char *str2 = "函数就是用于将hello进行转换";
char newstr[MAX_LEN];
TranslateSP tsp;
vert("utf-8", "gbk", str, strlen(str), newstr, MAX_LEN);
printf("%s\n", newstr);
TranslateSP newtsp("UTF-8", "GBK");
anslate(str1, strlen(str1), newstr, MAX_LEN);moderate
printf("%s\n", newstr);
anslate(str2, strlen(str2), newstr, MAX_LEN);
printf("%s\n", newstr);
return 0;
}
//编译:
g++ translateSP.cpp -o test
./test
我爱zhong国! %#@#培训私人教练
i大量需要转换的编码
函数就是用于将hello进行转换
英文在线转换器 (以上输出是GBK编码)