The current popular character encoding format is: Us-ascii, iso-8859-1, UTF-8, Utf-16be, Utf-16le, UTF-16, GBK, GB2312, GBK, GB2312 is specialized in processing Chinese encoding. The Libiconv is a common encoding conversion library, which supports the conversion between many commonly used encodings. Under Linux, the tool chain GCC has a dedicated Libiconv library, all without porting, but less fortunate under Arm-linux, which requires an additional porting of the Libiconv library, but the process is relatively straightforward.
One, character encoding conversion Libiconv Library Introduction
Libiconv is a common encoding conversion library that supports conversions between multiple encodings that are commonly used. The main functions are:
#include <iconv.h>
Iconv_ticonv_open (Constchar *tocode, const char *fromcode);
Size_ticonv (iconv_t CD, Char **inbuf, size_t *inbytesleft, Char **outbuf, size_t *outbytesleft);
int Iconv_close (ICONV_TCD);
Iconv_open is to open a coded stream, which is similar to opening an encoded pipe (channel), and returns 1 if an error occurs;
The iconv is used for specific input conversions, or 1 if an error occurs, otherwise 0 is returned;
Iconv_close is to close the pipe (channel).
If for a large number of encoding needs to be converted, the above function covert is not suitable for this way, one is the memory limit can not be called once, second, if multiple calls will frequently open a coded pipeline (channel), resulting in waste of resources, the best way is to take apart the function according to the situation use.
Second, arm-linux transplant process
1) http://www.gnu.org/software/libiconv/Download libiconv-1.14.tar.gz
2)./configure CC=ARM-HISMALL-LINUX-GCC--target=arm-linux--host=arm-hismall-linux--prefix= $PWD/out-- Enable-shared--enable-static
3) Make
4) make install
When the above command is completed, a new out directory is generated under the Libiconv directory, where 4 directories exist:
Bin include Lib Share
In the Lib directory for the generated library file, one of which is preloadable_libiconv.so, download it to the Development Board
In the Lib directory, and then set the system environment variables for the Development Board:
$ Export ld_preload=/lib/preloadable_libiconv.so
Then execute the application again and run normally.
Iii. Examples of Unicode.cpp
//#include <iconv.h>#include <iostream>#include<string.h>#include"./iconv.h"using namespacestd;//Code Conversion Action ClassesclassCodeconverter {Private: iconv_t cd; Public: //ConstructionCodeconverter (Const Char*from_charset,Const Char*To_charset) {CD=Iconv_open (To_charset, From_charset); } //Destruction~Codeconverter () {iconv_close (CD); } //Conversion Output intConvertChar*inbuf,intInlen,Char*outbuf,intOutlen) { Char**pin = &Inbuf; Char**pout = &Outbuf; memset (Outbuf,0, Outlen); returnIconv (CD, PIN, (size_t *) &inlen, pout, (size_t *) &Outlen); }};intMainintargcChar**argv) { Char*gb2312 ="I am a Chinese"; Charout0[ the] = {0}; Charout1[ the] = {0}; //Gb2312-->utf-8Codeconverter cc0 = Codeconverter ("gb2312","Utf-8"); Cc0.convert (gb2312, strlen (gb2312)+1, Out0, the); cout<<"Gb2312-->utf-8 in="<< gb2312 <<", out0="<< out0 <<Endl; //utf-8-->gb2312Codeconverter cc1 = Codeconverter ("Utf-8","gb2312"); Cc1.convert (Out0, strlen (out0)+1, OUT1, the); cout<<"utf-8-->gb2312 in="<< out0 <<", out1="<< OUT1 <<Endl; return 0;}
arm-hismall-linux-g++ unicode.cpp-l./-liconv //Static library then NFS downloads to the board or runs directly on the OK.
If you encounter a iconv conversion exception, you can use Perror ("Iconv") to see the error prompt, the above code I do under FreeBSD Utf-8-gb2312 conversion failed, because the Iconv_open two parameters are lowercase, and through the man Iconv or iconv-l see that all encoded strings supported by ICONV are uppercase.
Workaround for Iconv call failure on ARM Development Board