移植 iconv

PURPOSE: iconv provides functions that can change codeset from one to another but the working board doesn't not have the functions that <iconv.h> includes, so we add the iconv.h
  1. download libiconv-1.14.tar.gz
  2. read the INSTALL.generic
  3. according to the INSTALL.generic we generate the following install code
    # !/bin/bash
    mkdir /tmp/iconv
    ./configure --host=mipsel-linux CC= " mipsel-linux-gcc "  --prefix= " /tmp/iconv " --with-configuredir= " /tmp/iconv " 
    make 
    make install
  4. then we generate what we need in /tmp/iconv
  5. test code
    ExpandedBlockStart.gif View Code
    #include  " /tmp/iconv/include/iconv.h "
    #include <stdio.h>
    #include <stdlib.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== 0return - 1;
        memset(outbuf, 0,outlen);
         // if (iconv(cd,pin,&((size_t *)inlen),pout,&((size_t *)outlen) )==-1) return -1;
         if (iconv(cd,pin,((size_t *)(&inlen)),pout,((size_t *)(&outlen)) )==- 1return - 1;

        iconv_close(cd);
         return  0;
    }
    // UNICODE码转为GB2312码
    int u2g( char *inbuf, int inlen, char *outbuf, int outlen)
    {
         return code_convert( " utf-8 ", " gbk ",inbuf,inlen,outbuf,outlen);
    }
    // GB2312码转为UNICODE码
    int g2u( char *inbuf,size_t inlen, char *outbuf,size_t outlen)
    {
         return code_convert( " gb2312 ", " utf-8 ",inbuf,inlen,outbuf,outlen);
    }

    main()
    {
         char *in_utf8[ 6] =   { " 01 26涓 ", " 02 瀹跺涵鎴愬憳 "" 03 姘存灉 ", " 04 鏁板瓧1鍒 ", " 05 浜斿畼 " ,  " 06 中文 "};

         char  out[OUTLEN];
         int rec ;

         // unicode码转为gb2312码
         for( int i= 0;i< 6;i++)
        {
             // rec = u2g(in_utf8[i],strlen(in_utf8[i]),out,OUTLEN);
            rec = u2g(in_utf8[i], 255, out,OUTLEN);
            printf( " unicode-->gb2312 out=%s\n ", out);
        }
    }
  6. Makefile
    all:
        mipsel-linux-g++ -w -g conv.cpp -o conv -I/tmp/iconv/include/ -L/tmp/iconv/lib/ -liconv

we can use use it now , after we do the following steps:
copy the libs that generated in /tmp/iconv

cd /usr/local/bin/
export PATH=LD_LIBRARY= $TOOLCHAIN./
./conv

here is the end of this note

libconv在windows系统的移植,编译,测试工程代码 确实有需要的请下载使用。以下是测试代码 #include <stdlib.h> //include <sys/ipc.h> #include <stdio.h> #include <string.h> //#include <fcntl.h> //include <unistd.h> #include <iconv.h> int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen) { iconv_t cd; char **pin = &inbuf; char **pout = &outbuf; size_t iconv_ret; cd = iconv_open(to_charset,from_charset); if (cd==(iconv_t)-1) { perror("iconv_open():"); exit(0); } memset(outbuf,0,outlen); iconv_ret = iconv(cd,pin,&inlen,pout,&outlen); if (iconv_ret == (size_t)-1) { perror("iconv():"); exit(0); } iconv_close(cd); return 0; } int u2g(char *inbuf,int inlen,char *outbuf,int outlen) //UNICODE码转为GB2312码 { return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen); } int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen) //GB2312码转为UNICODE码 { return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen); } /* 这是一个iconv的测试例程: “纵海杯”东南大学第三届嵌入式系统设计邀请赛 */ int main(void) { unsigned char in_utf8[] = {0x20,0xe8,0xbf,0x99,0xe6,0x98,0xaf,0xe4,0xb8,0x80,0xe4,0xb8,0xaa,0x69,0x63,0x6f, 0x6e,0x76,0xe7,0x9a,0x84,0xe6,0xb5,0x8b,0xe8,0xaf,0x95,0xe4,0xbe,0x8b,0xe7,0xa8, 0x8b,0xef,0xbc,0x9a,0x0a,0xe2,0x80,0x9c,0xe7,0xba,0xb5,0xe6,0xb5,0xb7,0xe6,0x9d, 0xaf,0xe2,0x80,0x9d,0xe4,0xb8,0x9c,0xe5,0x8d,0x97,0xe5,0xa4,0xa7,0xe5,0xad,0xa6, 0xe7,0xac,0xac,0xe4,0xb8,0x89,0xe5,0xb1,0x8a,0xe5,0xb5,0x8c,0xe5,0x85,0xa5,0xe5, 0xbc,0x8f,0xe7,0xb3,0xbb,0xe7,0xbb,0x9f,0xe8,0xae,0xbe,0xe8,0xae,0xa1,0xe9,0x82, 0x80,0xe8,0xaf,0xb7,0xe8,0xb5,0x9b,0x00}; //UTF-8编码 char in_gb2312[] =" 这是一个iconv的测试例程:\n\ “纵海杯”东南大学第三届嵌入式系统设计邀请赛"; //GB2312编码 char out[256]; int rc; printf("\r\n------iconv test------\r\n"); printf("\r\n---Start UTF8 to GB2312 convert...\r\n"); rc = u2g(in_utf8,strlen(in_utf8),out,256); printf("%s\n",out); rc = strcmp(in_gb2312,out); if(rc==0) { printf("---End UTF8 to GB2312 convert OK\r\n"); } else { printf("---End UTF8 to GB2312 convert FAIL\r\n"); } printf("\r\n---Start GB2312 to UTF8 convert...\n"); rc = g2u(in_gb2312,strlen(in_gb2312),out,256); printf("%s\n",out); rc = strcmp(in_utf8,out); if(rc==0) { printf("---End GB2312 to UTF8 convert OK\r\n"); } else { printf("---End GB2312 to UTF8 convert FAIL\r\n"); } printf("\r\n------iconv test over------\r\n"); system("pause");; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值