Linux character encoding conversion UTF8 turn GB3212

Source: Internet
Author: User

When encoding and converting on Linux, you can either use the ICONV function family programming or use the ICONV command, except that the latter is for the file, and the specified file will be converted from one encoding to another.
First, using the ICONV function family for encoding conversion
Iconv function Family of the first article on the Linux encoding conversion, can either use the ICONV function family programming implementation, can also use the Iconv command to implement, but the latter is for the file, will be the specified file from one encoding to another encoding.
First, using the ICONV function family for encoding conversion
The header file of the Iconv function family is iconv.h, which needs to be included before use.
#include <iconv.h>
The Iconv function family has three functions, which are prototyped as follows:
(1) iconv_t iconv_open (const char *tocode, const char *fromcode);
This function describes which two encodings will be converted, Tocode is the target encoding, Fromcode is the original encoding, and the function returns a transform handle for use by the following two functions.
(2) size_t iconv (iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);
This function reads the characters from the inbuf and outputs them to outbuf, inbytesleft to record the number of characters that have not yet been converted, outbytesleft to record the remaining space of the output buffer. (3) int iconv_close (iconv_t CD);
This function closes the transform handle and frees the resource.
Example 1: Conversion sample program implemented in C language

/* F.C: Code conversion Sample C program */
#include <iconv.h>
#define Outlen 255
Main ()
{
Char *in_utf8 = "Shu E?ㄥ??" Turtle? ";
Char *in_gb2312 = "Installing";
Char Out[outlen];

Unicode code converted to gb2312 code
rc = u2g (In_utf8,strlen (In_utf8), Out,outlen);
printf ("unicode-->gb2312 out=%sn", out);
gb2312 code converted to Unicode code
rc = g2u (In_gb2312,strlen (in_gb2312), Out,outlen);
printf ("Gb2312-->unicode out=%sn", out);
}
Transcoding: Converting from one encoding to another
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==0) return-1;
memset (Outbuf,0,outlen);
if (Iconv (Cd,pin,&inlen,pout,&outlen) ==-1) return-1;
Iconv_close (CD);
return 0;
}
Unicode code converted to GB2312 code
int u2g (char *inbuf,int inlen,char *outbuf,int Outlen)
{
Return Code_convert ("Utf-8", "gb2312", Inbuf,inlen,outbuf,outlen);
}
GB2312 code converted to Unicode code
int g2u (char *inbuf,size_t inlen,char *outbuf,size_t Outlen)
{
Return Code_convert ("gb2312", "Utf-8", Inbuf,inlen,outbuf,outlen);
}

Example 2: Conversion sample program implemented in C + + language

/* F.cpp: Code Conversion sample C + + program */
#include <iconv.h>
#include <iostream>

#define Outlen 255

using namespace Std;

Code Conversion Action Classes
Class Codeconverter {
Private
iconv_t cd;
Public
Structure
Codeconverter (const char *from_charset,const char *to_charset) {
cd = Iconv_open (To_charset,from_charset);
}

Destruction
~codeconverter () {
Iconv_close (CD);
}

Conversion output
int convert (char *inbuf,int inlen,char *outbuf,int outlen) {
Char **pin = &inbuf;
Char **pout = &outbuf;

memset (Outbuf,0,outlen);
Return Iconv (Cd,pin, (size_t *) &inlen,pout, (size_t *) &outlen);
}
};

int main (int argc, char **argv)
{
Char *in_utf8 = "Shu E?ㄥ??" Turtle? ";
Char *in_gb2312 = "Installing";
Char Out[outlen];

utf-8-->gb2312
Codeconverter cc = Codeconverter ("Utf-8", "gb2312");
Cc.convert (In_utf8,strlen (In_utf8), Out,outlen);
cout << "utf-8-->gb2312 in=" << in_utf8 << "out=" << out << Endl;

Gb2312-->utf-8
Codeconverter CC2 = Codeconverter ("gb2312", "utf-8");
Cc2.convert (In_gb2312,strlen (in_gb2312), Out,outlen);
cout << "Gb2312-->utf-8 in=" << in_gb2312 << "out=" << out << Endl;
} Linux C Character set conversion, utf-8,gb2312 recently to help friends write a system interface of small East, 2 system character sets, a use of UTF-8, a GB2312, forced to convert the character set. The conversion function is recorded as follows: #include <iconv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Outlen 255
Main ()
{
char *in_utf8 = "UTF8 string";
Char *in_gb2312 = "\XBE\XB2\XCC\XAC\XC4\XA3\XCA\XBD";

Char Out[outlen];
int rec;

Unicode code converted to gb2312 code
rec = u2g (In_utf8,strlen (In_utf8), Out,outlen);
printf ("unicode-->gb2312 out=%s\n", out);

gb2312 code converted to Unicode code
rec = g2u (In_gb2312,strlen (in_gb2312), Out,outlen);
printf ("Gb2312-->unicode out=%s \ n", out);
}
Transcoding: Converting from one encoding to another
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==0) return-1;
memset (Outbuf,0,outlen);
if (Iconv (Cd,pin,&inlen,pout,&outlen) ==-1) return-1;
Iconv_close (CD);
return 0;
}
Unicode code converted to GB2312 code
int u2g (char *inbuf,int inlen,char *outbuf,int Outlen)
{
Return Code_convert ("Utf-8", "gb2312", Inbuf,inlen,outbuf,outlen);
}
GB2312 code converted to Unicode code
int g2u (char *inbuf,size_t inlen,char *outbuf,size_t Outlen)
{
Return Code_convert ("gb2312", "Utf-8", Inbuf,inlen,outbuf,outlen);

Linux character encoding conversion UTF8 turn GB3212

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.