ARM开发板上iconv_open("utf-8", "gb2312") 调用失败的解决方法:
1. 先下载转码库:libiconv-1.14.tar.gz
2. 假设交叉编译工具arm-gcc,安装在 /usr/local/arm
编译命令arm-linux-gcc,的环境变量是:/usr/local/arm/gcc-4.4.4-glibc-2.11.1-multilib-1.0/arm-fsl-linux-gnueabi/bin
3. 解压缩libiconv-1.14.tar.gz,进入目录libiconv-1.14,
4. 配置、编译、安装:
# ./configure --host=arm-linux --prefix=/usr/local/arm/iconv CC=arm-linux-gcc LDFLAGS="-L/usr/local/arm/gcc-4.4.4-glibc-2.11.1-multilib-1.0/arm-fsl-linux-gnueabi/lib" --enable-static
# make
# make install
5. 在/usr/local/arm/iconv中,将lib/libiconv.a和include/iconv.h拷贝到自己的程序目录里使用即可。
注意:包含iconv.h头文件时用#include "iconv.h",不要用#include
封装的编码转换函数:
//编码转换:从一种编码转为另一种编码
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 == -1)
{
printf("iconv_open error\n");
return -1;
}
memset(outbuf,0,outlen);
if (iconv(cd,pin,(size_t*)&inlen,pout,(size_t*)&outlen) == -1)
{
printf("iconv error\n");
iconv_close(cd);
return -1;
}
iconv_close(cd);
return 0;
}
ARM平台下,读卡器读取的身份证文字信息,需要从Unicode转为GBK
能成功的转码方式:
Unicode:UCS-2-INTERNAL , UTF-16LE
中文:GB18030 ,GBK,GB2312
code_convert("UCS-2-INTERNAL","GB18030",Info, sizeof(Info),cANSIStr, sizeof(cANSIStr));