关于利用 iconv 跨平台转编码问题

string name = "Ambition";

GBKToUTF8(name,"GBK","UTF-8");  //需要转换什么编码只需要更改后面2个参数。万能的(嘎嘎。。。。)

游戏开发群:44727718

int JCStrCoding::GBKToUTF8(string &gbkStr,const char* toCode/*="gbk"*/,const char* fromCode/*="utf-8"*/)
{
iconv_t iconvH;
iconvH
= iconv_open(fromCode,toCode);
if(iconvH == 0)
return -1;
const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLenth
= gbkStr.length();
char *outbuf = (char *)malloc(strLenth *4);

char* pBuff = outbuf;

memset(outbuf,
0,strLenth*4);
size_t outLength
= strLenth*4;

if( -1 == iconv(iconvH,pin,&strLenth,&outbuf,&outLength))
{
iconv_close(iconvH);
return -1;
}
gbkStr
= pBuff;
iconv_close(iconvH);
return 0;
}

  这个 iconv 库到网上去下个就行了。。只需使用他的lib 跟头文件就可以 。。。。

原文地址:https://www.cnblogs.com/GameDeveloper/p/2114735.html