怎样学习使用libiconv库

    libiconv库是一个基于GNU协议的开源库,主要是解决多语言编码处理转换等应用问题。
    怎样学习使用libiconv库?对于刚接触到人来说,这篇文章不妨去看一看,若已经用到过该库的人,在应用的过程中可能遇到一些问题,我们可以一起来探讨,我的联系方式是 cnangel@gmail.com

    几个函数原型:
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_close(iconv_t cd);
    其中:
iconv_open是打开一个编码流,类似于打开一个编码管道(通道),出错则返回 -1;
iconv用于具体输入的转换,如果出错,则返回 -1,否则返回 0;
iconv_close是关闭该管道(通道)。
    举个例子:
#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;
    }
    memset(output, 0, olen);
    if (iconv(cd, pin, &ilen, pout, &olen)) return -1;
    iconv_close(cd);
    return 0;
}
    这里面covert函数就是用于将编码进行转换,其中要注意的地方是iconv函数的传递参数:
1,iconv传递有5个参数;
2,第3个参数和第5个参数一般是input和output实际分配的大小,一般是 sizeof(type)*strlen(string);
3,第4个参数是不能直接传递指针的地址,因为iconv函数能够改变指针的值,所以需要复制一份指针变量;
    如果对于大量需要转换的编码,上述函数covert不适合该方式,一是内存的限制不能一次调用,二是若分多次调用会频繁打开一个编码管道(通道),导致资源浪费,最好的办法还是拆开该函数根据情况使用。
    这里补充一下代码:
translateSP.h:
 #ifndef __TRANSLATESP_H_
 #define __TRANSLATESP_H_
 #include <iconv.h>
 
 class TranslateSP
 {
     public:
         TranslateSP():i_cd(0){}
         TranslateSP(const char *from_charset,const char *to_charset)
         {
             i_cd = iconv_open(to_charset, from_charset);
             if ((iconv_t)-1 == i_cd) printf("iconv open error!\n");
         }
         ~TranslateSP()
         {  
             if (i_cd)
                 iconv_close(i_cd);
         }
 
     public:
         size_t translate(char *src, size_t srcLen, char *desc, size_t descLen);
         size_t convert(const char *from_charset, const char *to_charset,
                 char *src, size_t srcLen, char *desc, size_t descLen);
 
     private:
         iconv_t i_cd;
 };
 
 #endif
translateSP.cpp:
 #include "translateSP.h"
 
 #define MAX_LEN 200
 
 size_t TranslateSP::translate(char *src, size_t srcLen, char *desc, size_t descLen)
 {
     char **inbuf = &src;
     char **outbuf = &desc;
     memset(desc, 0, descLen);
     return iconv(i_cd, inbuf, &srcLen, outbuf, &descLen);
 }
 
 size_t TranslateSP::convert(const char *from_charset, const char *to_charset,
         char *src, size_t srcLen, char *desc, size_t descLen)
 {
     char **inbuf = &src;
     char **outbuf = &desc;
     iconv_t cd = iconv_open(to_charset, from_charset);
     if ((iconv_t)-1 == cd) return (size_t)-1;
     memset(desc, 0, descLen);
     size_t n = iconv(cd, inbuf, &srcLen, outbuf, &descLen);
     iconv_close(cd);
     return n;
 }
 
 int main(int argc, char *argv[])
 {
     char *str = "我爱zhong国! %#@#";
     char *str1 = "i大量需要转换的编码";
     char *str2 = "函数就是用于将hello进行转换";
     char newstr[MAX_LEN];
     TranslateSP tsp;
     tsp.convert("utf-8", "gbk", str, strlen(str), newstr, MAX_LEN);
     printf("%s\n", newstr);
     TranslateSP newtsp("UTF-8", "GBK");
     newtsp.translate(str1, strlen(str1), newstr, MAX_LEN);
     printf("%s\n", newstr);
     newtsp.translate(str2, strlen(str2), newstr, MAX_LEN);
     printf("%s\n", newstr);
     return 0;
 }
编译:
g++ translateSP.cpp -o test
./test
我爱zhong国! %#@#
i大量需要转换的编码
函数就是用于将hello进行转换
(以上输出是GBK编码)


Monthly Archives

Pages

Powered by Movable Type 7.7.2

About this Entry

This page contains a single entry by Cnangel published on February 27, 2008 3:45 PM.

php扩展实战 ------ 获得ip的来源地址 was the previous entry in this blog.

和何伟平老师的一番谈话 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.