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