vs 中 cocos2d-x 中文乱码

整了将近两天终于暂时解决了vs 中 cocos2d-x 中文乱码的问题。

解决办法:

1. 在vs创建.h文件和.cpp文件的时候,敲代码前就要保存成utf8无签名格式,不然敲了代码再保存可能出问题;

2. 从外部文件中读入中文;

3. 用cocos2d-x自带的库转码;

用法:创建utf8.h和utf8.cpp

utf8.h代码:

#ifdef WIN32
#define U(str) GBKToUTF8(str)
#else
#define U(str) str
#endif


#ifdef WIN32
#include "iconv/iconv.h"
#include <string.h>
#include <stdlib.h>


extern const char* GBKToUTF8(const char *strChar);
#endif

---------------------------------------------

utf8.cpp代码:

#include "utf8.h"
static  char g_GBKConvUTF8Buf[5000] = {0};
const char* GBKToUTF8(const char *strChar)
{
iconv_t iconvH;
iconvH = iconv_open("utf-8","gb2312");


if (iconvH == 0)
{
return NULL;
}


size_t strLength = strlen(strChar);
size_t outLength = strLength<<2;
size_t copyLength = outLength;
memset(g_GBKConvUTF8Buf, 0, 5000);
char* outbuf = (char*) malloc(outLength);
char* pBuff = outbuf;
memset( outbuf, 0, outLength);


if (-1 == iconv(iconvH, &strChar, &strLength, &outbuf, &outLength))
{
iconv_close(iconvH);
return NULL;
}


memcpy(g_GBKConvUTF8Buf,pBuff,copyLength);
free(pBuff);
iconv_close(iconvH);
return g_GBKConvUTF8Buf;
}

然后用到中文的地方引入utf8.h头文件后,调用U()函数就行了比如:

CCLabelTTF *  _label = CCLabelTTF::create(U("中文 "), "微软雅黑",32);

注:头文件中的extern关键字很重要!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值