Use the libiconv library for transcoding in linux and the linuxlibiconv Library
Iconv command to achieve character set encoding conversion in linux windows files copied to linux often garbled, because in windows file encoding for GBK, linux default file encoding for UTF-8, therefore, the libiconv library is required for transcoding. 1. the iconv command is used as follows: iconv [option...] [file...] input/output format specifications:-f, -- from-code = Name original text encoding-t, -- to-code = Name output encoding information:-l, -- list lists all known character set output control:-c ignores invalid characters-o from the output, -- output = FILE output FILE-s, -- silent close warning -- verbose print progress information to convert the GBK-encoded file Hosts_GBK to the UTF-8-encoded HOSTS_UTF8. example: iconv-f GBK-t UTF-8 Hosts_GBK> HOSTS_UTF8 2. iconv function:
(1) iconv_t iconv_open (const char * tocode, const char * fromcode );
This function indicates which two types of encoding are to be converted. tocode is the target encoding and fromcode is the original encoding. This function returns a conversion handle for the following two functions.
Http://linux.die.net/man/3/iconv_open
(2) size_t iconv (iconv_t cd, char ** inbuf, size_t * inbytesleft, char ** outbuf, size_t * outbytesleft );
This function reads characters from inbuf and outputs the converted characters to outbuf. inbytesleft records the number of characters that have not been converted, and outbytesleft records the remaining space of the output buffer.
Http://pubs.opengroup.org/onlinepubs/009695399/functions/iconv.html
In most casesInbufNot NULL,* InbufOr NULL. In this case,IconvThe function will use* InbufThe starting multi-byte sequence is converted* OutbufThe starting multi-byte sequence. Slave* InbufStart reading, maximum* InbytesleftByte, after conversion, from* OutbufStart writing, up* OutbytesleftBytes.
The iconv function converts a multi-byte character each time,* InbufIncrease the number of converted bytes,* InbytesleftCorrespondingly, the number of converted bytes is reduced. Correspondingly,* OutbufAnd* OutbytesleftModify and modifyCdTherefore, the corresponding operation is for its copy.
Conversion cannot be completed in the following four cases:
1. The input contains invalid multi-byte sequences. In this caseErrnoSetEILSEQAnd return (size_t) (-1 ).* InbufPoint to the leftmost end of the invalid sequence.
2. All input byte sequences have been converted, that is* InbytesleftReduce to 0. At this time,IconvReturns the number of conversions completed in this call (reversible conversions are not counted ).
3. The input end with an incomplete multi-byte sequence. In this caseErrnoSetEINVALAnd return (size_t) (-1 ).* InbufPoint to the leftmost end of an incomplete multi-byte sequence.
4. The output cache area does not have enough space to store the next character. In this caseErrnoSetE2BIGAnd return (size_t) (-1 ).
Another scenario isInbufIs NULL or* InbufIs NULL,* OutbufNot NULL,* OutbufOr NULL. In this case, the iconv function triesCdAnd store a corresponding shift sequence* Outbuf. Slave* OutbufStart, write at most* OutbytesleftBytes. If the output cache area does not have enough space to store the reset sequenceErrnoSetE2BIGAnd return (size_t) (-1 ). Conversely,* OutbufIncrease the number of written bytes and* OutbytesleftReduce the number of bytes written.
The third scenario isInbufIs NULL or* InbufIs NULL,* OutbufIs NULL or* OutbufIs NULL. In this case, the iconv function triesCdTo the initial status.
Return Value:
The iconv function returns the number of characters converted in this call. reversible conversion is not counted. When an error occurs, it will modifyErrnoAnd return (size_t) (-1 ).
Error:
E2BIG* OutbufThere is not enough space.
The EILSEQ input contains invalid multi-byte sequences.
The EINVAL input contains an incomplete multi-byte sequence.
(3) int iconv_close (iconv_t cd );
This function is used to close the conversion handle and release resources.
C sample code
// Convert UTF-8 to gb2312. The code is for reference only.
Iconv_t cd; char src_utf8 [20] = "utf8 encoding"; char * inbuf = src_utf8; int inen = strlen (inbuf); int outlen = 255; char * outbuf = (char *) malloc (outlen); cd = iconv_open ("gb2312", "UTF-8"); iconv (cd, & inbuf, (size_t *) & inlen, & outbuf, & outlen); printf ("% s \ n", outbuf); iconv_close (cd); free (outbuf );
In Linux, besides the iconv function, which function can be converted?
This is mainly because the code table is troublesome. Fortunately, it is ready-made.
Hi.baidu.com/..e.html
It's hard to figure out if many libraries have been installed in inglinux ubuntu and you still cannot run the program.
../Bin
These three. so files may be generated by the project itself or carry libraries under the bin of the first-level directory in Makefile.
If the three. so files are not in the bin, check the Makefile and regenerate the three shared libraries.