linux iconv函数失败,Linux 编码转换 (iconv失败的解决方法)

一开始,调用iconv_open()会返回-1,使用printf("errno=%d\n",errno)查看,返回22,说是invalid argument参数非法。

试了下,确定不是UTF-8或utf-8的大小写问题。然后按博客http://blog.csdn.net/love_life2011/article/details/7086910#comments所述,更新iconv函数簇为较新版本后即可。

附http://blog.csdn.net/love_life2011/article/details/7086910#comments内容:

相同的C源程序,在ubuntu11.10上能够正常执行,而在ARM开发板则不能正常执行,调用 iconv_open("utf-8", "gb2312") 返回失败,错误信息为“Invalid argument”. 经过查询资料得知iconv相关函数为libc中的函数,初步分析得出结论为有可能是libc版本中 iconv相关函数的版本不同造成的,因此要更新iconv相关函数。 更新iconv相关函数有两种方法: 第一,更新libc库; 第二,更新libiconv库。 第一种方法更新libc库比较麻烦,因为我们用的是编译好的交叉编译器,这中方法需要重新编译生成

交叉编译器,并且也需要使用新编译生成的交叉编译工具重新编译应用程序,因此本方法代价太大,

采用第二种方法。

第二种方法为只更新libiconv库,到iconv官网下载最新的库源码包,下载地址为:

http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz

解压后得到libiconv目录,阅读该目录下Readme文件得出,编译安装libiconv库也有两种方式:

This library can be built and installed in two variants:

- The library mode. This works on all systems, and uses a library

`libiconv.so' and a header file `'. (Both are installed

through "make install".)

To use it, simply #include and use the functions.

To use it in an autoconfiguring package:

- If you don't use automake, append m4/iconv.m4 to your aclocal.m4

file.

- If you do use automake, add m4/iconv.m4 to your m4 macro repository.

- Add to the link command line of libraries and executables that use

the functions the placeholder @LIBICONV@ (or, if using libtool for

the link, @LTLIBICONV@). If you use automake, the right place for

these additions are the *_LDADD variables.

Note that 'iconv.m4' is also part of the GNU gettext package, which

installs it in /usr/local/share/aclocal/iconv.m4.

- The libc plug/override mode. This works on GNU/Linux, Solaris and OSF/1

systems only. It is a way to get good iconv support without having

glibc-2.1.

It installs a library `preloadable_libiconv.so'. This library can be used

with LD_PRELOAD, to override the iconv* functions present in the C library.

On GNU/Linux and Solaris:

$ export LD_PRELOAD=/usr/local/lib/preloadable_libiconv.so

On OSF/1:

$ export _RLD_LIST=/usr/local/lib/preloadable_libiconv.so:DEFAULT

A program's source need not be modified, the program need not even be

recompiled. Just set the LD_PRELOAD environment variable, that's it!

我在ARM开发板上采用“The libc plug/override mode”实验成功,下面介绍编译过程:

在libiconv目录下:

$./configure --prefix=$PWD/out --host=arm-linux

$make

$make install

上述命令执行完成后会在libiconv目录下生成新的out目录,该目录下存在4个目录分别是:

bin include lib share

在lib目录下为生成的库文件,其中一个为preloadable_libiconv.so,把它下载到开发板

的lib目录下,然后再设置开发板的系统环境变量:

$ export LD_PRELOAD=/lib/preloadable_libiconv.so

然后再执行应用程序即可,正常运行。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
嵌入式 Linux 中使用 iconv 进行字符编码转换的步骤如下: 1. 包含头文件:在源代码中包含 `<iconv.h>` 头文件,这是进行字符编码转换所需的函数和数据结构的声明。 2. 打开转换句柄:使用 `iconv_open` 函数打开一个字符编码转换句柄。该函数的原型为: ``` iconv_t iconv_open(const char* tocode, const char* fromcode); ``` 其中 `tocode` 是目标编码,`fromcode` 是源编码。可以使用 `iconv --list` 命令查看系统支持的编码。 3. 设置转换选项:可以通过 `iconvctl` 函数设置一些转换选项,例如忽略无法转换的字符或错误处理策略。这一步骤可以根据需求来选择,不是必须的。 4. 初始化转换状态:使用 `iconv` 函数初始化字符编码转换状态。该函数的原型为: ``` size_t iconv(iconv_t cd, char** inbuf, size_t* inbytesleft, char** outbuf, size_t* outbytesleft); ``` 其中 `cd` 是之前打开的转换句柄,`inbuf` 是输入缓冲区指针,`inbytesleft` 是输入缓冲区大小,`outbuf` 是输出缓冲区指针,`outbytesleft` 是输出缓冲区大小。 5. 进行编码转换:使用 `iconv` 函数进行字符编码转换。反复调用该函数,直到转换完成或出错。每次调用后,输入缓冲区的指针和大小会更新,输出缓冲区的指针和大小也会更新。 6. 关闭转换句柄:使用 `iconv_close` 函数关闭字符编码转换句柄。该函数的原型为: ``` int iconv_close(iconv_t cd); ``` 其中 `cd` 是之前打开的转换句柄。 以上是使用 iconv 进行字符编码转换的基本步骤。在实际使用中,你需要根据具体的需求来设置输入和输出缓冲区,并根据返回值来处理转换结果或错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值