GBK to Utf-8 iconv encoding conversion

Source: Internet
Author: User

Under Linux sometimes the characters need to be encoded and converted (crawlers convert GBK to UTF-8 encoding ...). ), you can generally choose the Iconv function.

Input under Terminal

Mans 3 Iconv

The method of using Iconv function is obtained.

Personally, after the MSDN documentation, it feels less cool to look at the documentation below Linux.

Use the Iconv function for transcoding, generally using three functions: Iconv_open, Iconv, iconv_close three functions.

iconv_t iconv_open (const char* tocode,const char* Fromcode)

The return value is something like a file handle, Tococode: Target encoding, Fromcode: source code.

Below the terminal enter the following command to get the code supported by the system:

Iconv--list


Then there is the transcoding function:

size_t Iconv (iconv_t CD,             char **inbuf, size_t *inbytesleft,            char **outbuf, size_t *outbytesleft);

CD: Just Iconv_open get the handle, Inbuf: A pointer to the string address that needs transcoding, Inbytesleft: The length of the transcoding required, Outbuf: Output space, Outbytesleft: Remaining space

Specific function content can view this page Iconv_open iconv iconv_close function document


After the use is complete, you need to close the previously opened handle:

int Iconv_close (iconv_t CD);

Example:
Header file: CTranstlateString.h
#ifndef ctranstlatestring_h#define ctranstlatestring_h#include <string> #include <iostream> #include < Iconv.h>class ctranstlatestring{public:ctranstlatestring (const char *to_encode, const char *from_encode); const char* Translate (const char* from, size_t Flen);  String conversion virtual ~ctranstlatestring ();p rotected:private:char* fromstring; String char* tostring;   size_t fromleng;//size_t Toleng with converted string preparation length;  iconv_t handle;const char* intranlsate ();   True string function}; #endif//Ctranstlatestring_h


Files: CTranstlateString.cpp
#include <string.h> #include "CTranstlateString.h" using namespace std; ctranstlatestring::ctranstlatestring (const char *to_encode, const char *from_encode) {fromstring = new Char[1];fromlen    g = 1; ToString = new Char[1];toleng = 1;handle = Iconv_open (To_encode, From_encode);}    Ctranstlatestring::~ctranstlatestring () {delete[] Fromstring;fromleng = 0; delete[] Tostring;toleng = 0;iconv_close (handle);} Const char* ctranstlatestring::translate (const char* from, size_t Flen) {if (Fromleng < flen+1)//string to be encoded        Store up {delete[] fromstring;        fromstring = Null;fromleng = 0;        try {fromstring = new Char[flen+1];fromleng = Flen + 1;        } catch (...)            {fromstring = NULL;            Fromleng = 0;        return NULL;  }}memset (fromstring, 0, Fromleng); memcpy (FromString, from, Fromleng); size_t tlen = Flen * 2;//class-encoded string space if ( Toleng < Tlen +1) {delete[] Tostring;tostring = Null;toleng = 0;try{tostring = new Char[tlen + 1];toleng = Tlen + 1;} catch (...)    {tostring = Null;toleng = 0;return NULL;}  }memset (ToString, 0, Toleng); return intranlsate (); String transcoding}const char* ctranstlatestring::intranlsate () {size_t outlen = Toleng; char *inbuf = Fromstring;char *outbuf = tostr ing; size_t inlen = fromleng;if (-1 = = Iconv (handle, &inbuf, &inlen, &outbuf, &outlen)) {RET Urn "";}      return tostring; Note that the return here is the focus}







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

GBK to Utf-8 iconv encoding conversion

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.