分类: 嵌入式
2009-08-04 19:46:14
先下载libiconv库,我用的是libiconv-1.12,解压后进行交叉编译:
./configure --host=arm –prefix=/home/my/iconv/
CXX=arm -linux-gnu-g++ CC=arm -linux-gnu-gcc-
LDFLAGS="-L/opt/gcc-
Make
Make install
在/home/my/iconv/下的lib文件夹中就有了libiconv.a库以及include文件夹中的iconv.h;之后就可用它们来写转码程序:
#include
#include
#include
#include "iconv.h" //这里要用” “,不能用<>
class CodeConverter
{
private:
iconv_t cd;
public:
CodeConverter(const char *from,const char *to);
~CodeConverter();
// 转换输出
int convert(char *inbuf,int inlen,char *outbuf,int outlen);
};
-------------------------------------------------------------------------------------------------
#include "CodeConverter.h"
CodeConverter::CodeConverter(const char *from,const char *to)
{
cd = iconv_open(to,from);
if(cd == (iconv_t)-1)
printf("cd -1\n");
}
CodeConverter::~CodeConverter()
{
iconv_close(cd);
}
// 转换输出
int CodeConverter::convert(char *inbuf,int inlen,char *outbuf,int outlen)
{
char **pin = &inbuf;
char **pout = &outbuf;
memset(outbuf,0,outlen);
return iconv(cd,pin,(size_t *)&inlen,pout,(size_t *)&outlen);
}
#include "CodeConverter.h"
#include
int main(int argc, char **argv)
{
char *in_gb2312 = "http://
char out[1024];
// gb2312-->utf-8
CodeConverter cc2 = CodeConverter("gb2312","utf-8");
int r1 = cc2.convert(in_gb2312,strlen(in_gb2312),out,1024);
//cout << "r1="<
printf("r1:%d in:%s out:%s\n",r1,in_utf8,out);
}
arm -linux-gnu-gcc –c CodeConverter.c
arm -linux-gnu-gcc –o main CodeConverter.o main.c –L./ -liconv
问题是生成的文件有
PS:
作者使用的是静态链接库,也可以使用动态库,应该能减小体积.