GBK encoding to UTF-8 encoding for Linux platforms

Source: Internet
Author: User

In writing cocos need to parse a file, but the file is GBK encoding method, and Cocos default is to use UTF-8 so need to transcode, this is found on the Internet code, but the original can not be used, which I modified.

System Environment: Macmini

Compiler: Xcode

Game Engine: Cocos2d-x

Header files:#include <iconv.h>

Int rf4filedecode::code_convert (char *from_charset,char *to_charset,char *inbuf,int  Inlen,char *outbuf,int outlen) {    iconv_t cd;    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, (size_t*) &inlen,pout, (size_t*) &outlen)  == -1)         return -1;     iconv_close (CD);     return 0;} INT&NBSP;RF4FILEDECODE::U2G (Char *inbuf,int inlen,char *outbuf,int outlen) {     return code_convert (char*) "Utf-8", (char*) "gb2312", inbuf,iNlen,outbuf,outlen);} INT&NBSP;RF4FILEDECODE::G2U (Char *inbuf,size_t inlen,char *outbuf,size_t outlen) {     return code_convert ((char*) "gb2312", (char*) "Utf-8", Inbuf, (int) inlen,outbuf, (int) outlen) ;}

At the time of invocation, the original encoded string is best to be saved to the original buffer by separating it from the new encoded string by two instead of transcoding.

Calling code:

Char b[[1024x768];

This,g2u(buff,1024x768, B,1024x768);


GBK encoding to UTF-8 encoding for Linux platforms

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.