Created
April 11, 2017 06:28
-
-
Save mgttt/d0aba83da88552f95edafb65124d41c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ```c++ | |
| #include <iconv.h> //for gbk/big5/utf8 | |
| int code_convert(char *from_charset,char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t 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; | |
| } | |
| std::string any2utf8(std::string in,std::string fromEncode,std::string toEncode) | |
| { | |
| char* inbuf=(char*) in.c_str(); | |
| int inlen=strlen(inbuf); | |
| int outlen=inlen*3;//in case unicode 3 times than ascii | |
| char outbuf[outlen]={0}; | |
| int rst=code_convert((char*)fromEncode.c_str(),(char*)toEncode.c_str(),inbuf,inlen,outbuf,outlen); | |
| if(rst==0){ | |
| return std::string(outbuf); | |
| }else{ | |
| return in; | |
| } | |
| } | |
| std::string gbk2utf8(const char* in) | |
| { | |
| return any2utf8(std::string(in),std::string("gbk"),std::string("utf-8")); | |
| } | |
| ``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment