C language uses Iconv function to implement character encoding conversion __HTML5

Source: Internet
Author: User
Tags function prototype
C language using the Iconv function to implement character encoding conversion Linux provides a iconv library to implement character encoding conversion, first introduced the following command line: Iconv [f encoding] [-t encoding] [inputfile ...] This usage is relatively simple. There are three functions to use programmatically, and all three functions need to include header files #include <iconv.h>. respectively: Iconv_open,iconv,iconv_close. The general step is: First use Iconv_open to open a character conversion descriptor, use Iconv to perform the actual conversion, after the conversion is completed, use the Iconv_close to do the necessary cleanup work. Introduction function Use Method: 1 Iconv_open function prototype is:
iconv_t iconv_open (const char* tocode, const char* fromcode);
This function returns a conversion descriptor that converts a formcode encoded string into a tocode encoded string. If error returns (iconv_t)-1, set errno. 2) The Iconv function prototype is:
       size_t Iconv (iconv_t CD,
                    char **inbuf, size_t *inbytesleft,
                    char **outbuf, size_t *outbytesleft);
This function performs the actual conversion operation using the transformation descriptor returned by Iconv_open. Inbuf represents the need to convert the encoded character address, Inbytesleft represents the number of characters needed to convert the address, OUTBUF represents the need to convert the character store address, Outbytesleft represents the maximum number of characters to store the conversion of the address. This function modifies the incoming arguments, so you would use a different four variable to represent the address.
function execution failure Returns (size_t) -1,errno is set, otherwise, returns the number of characters that are converted in an irreversible manner, and the number of characters in reversible conversions is not counted. 3) Iconv_close
The function prototype is:
       int Iconv_close (iconv_t CD);
This function closes the descriptor returned by calling Iconv_open and frees up the memory space, and returns -1,errno if the success returns 0. Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iconv.h>

#define BUFSIZE 1024

int utf2gbk (char *buf, size_t len)
{
	iconv_t cd = Iconv_open ("GBK", "UTF-8");
	if (cd = = (iconv_t)-1) {
		perror ("Get character conversion descriptor failed.") \ n ");
		return-1;
	}
	size_t sz = bufsize * bufsize;
	Char *tmp_str = (char *) malloc (SZ);
	Do not pass the original pointer in, that will change the original pointer
	size_t inlen = len;
	size_t Outlen = sz;
	char *in = buf;
	char *out = tmp_str;
	if (tmp_str = = NULL) {
		iconv_close (CD);
		fprintf (stderr, "allocating memory failed.") \ n ");
		return-1;
	}
	memset (tmp_str, 0, SZ);
	if (Iconv (CD, &in, &inlen, &out, &outlen) = = (size_t)-1) {
		iconv_close (CD);
		return-1;
	}
	Iconv_close (CD);
	return 0;
}

int main (int argc, char *argv[])
{
	char in[bufsize] = "nihao1nih years later";
	int len = strlen (in);
	printf ("%s\n", in);
	UTF2GBK (in, Len);
	printf ("%s\n", in);
	return 0;
}


Reference: http://www.cnblogs.com/xuxm2007/archive/2010/11/09/1872379.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.