其实就是将字符串转换成 utf-8; 但在cocos下要判断平台,在安卓平台下,属于正常
下面是 在windwos平台下转换 utf8 的方法 ;;
文章需要用到 iconv的 源文件 和 库文件;;
下载地址 : http://download.csdn.net/detail/hubaolin/5983075
#include "Utils.h"
#include "iconv/iconv.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
int Utils::GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
{
iconv_t iconvH;
iconvH = iconv_open(formCode,toCode);
if(iconvH == 0)
{
return -1;
}
const char* strChar = gbkStr.c_str();
const char** pin = &strChar;
size_t strLength = gbkStr.length();
char* outbuf = (char*)malloc(strLength*4);
char* pBuff = outbuf;
memset(outbuf,0,strLength*4);
size_t outLength = strLength*4;
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))
{
iconv_close(iconvH);
return -1;
}
gbkStr = pBuff;
iconv_close(iconvH);
return 0;
}
const char* Utils::GBKToUTF(std::string &gbkStr)
{
this->GBKToUTF8(gbkStr,"gbk","utf-8");
return gbkStr.c_str();
}
#endif
下面是头文件
#ifndef _UTILS_H_
#define _UTILS_H_
#include "cocos2d.h"
class Utils
{
public:
//如果是windows平台; 处理Utf-8编码
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);
const char* GBKToUTF(std::string &gbkStr);
#endif
private:
};
#endif