要在头文件中加入iconv.h
在 debug.win32还有release.win32中加入iconv.lib charset.lib
在cocos2d-2.0-x-2.0.4\cocos2dx\platform\third_party\win32\下 建立一个iconv文件夹把iconv.h放进去
在项目属性,配置属性,连接器,输入 附加依赖项 中加入两个。lib
中文转换方法
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int 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;
}
#endif
上述方法是转换方法。
使用方法如下:
std::string titleStr = "是男人就坚持20秒";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(titleStr,"gb2312","utf-8");
#endif
CCLabelTTF* pLabel = CCLabelTTF::labelWithString(titleStr.c_str(), "Thonburi", 30);
在 debug.win32还有release.win32中加入iconv.lib charset.lib
在cocos2d-2.0-x-2.0.4\cocos2dx\platform\third_party\win32\下 建立一个iconv文件夹把iconv.h放进去
在项目属性,配置属性,连接器,输入 附加依赖项 中加入两个。lib
中文转换方法
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int 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;
}
#endif
上述方法是转换方法。
使用方法如下:
std::string titleStr = "是男人就坚持20秒";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(titleStr,"gb2312","utf-8");
#endif
CCLabelTTF* pLabel = CCLabelTTF::labelWithString(titleStr.c_str(), "Thonburi", 30);