首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

移栽 iconv

2012-09-21 
移植 iconvPURPOSE: iconv provides functions that can change codeset from one to another but the wor

移植 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
download libiconv-1.14.tar.gz
read the INSTALL.generic
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
then we generate what we need in /tmp/iconv
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==0) return -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)) )==-1) return -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);
}
}
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

热点排行