• 移植 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
      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
    7. 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

  • 相关阅读:
    Oracle分页SQL
    CentOS7下安装Anaconda3
    Alibaba分层领域模型规约
    java的continue标签
    SQLserver 及 redis 无法连接问题
    HTTP状态码
    java命令功能
    sql 查询结果自增序号
    Viewpage实现左右无限滑动
    Android OOM 问题的总结
  • 原文地址:https://www.cnblogs.com/no7dw/p/2193722.html
Copyright © 2020-2023  润新知