我当初在用cocos2d做黑白棋的时候用label无法正常的输出中文,但是英语和数字符号都是能正常显示的。后来我去查了一下原来是编码的问题,用vs的时候一般编码是GBK,而cocos2d是用UTF-8。所以当我们在vs里写好程序运行的时候发现出来的中文是乱码的。下面我来介绍两种能正常显示中文的方法。
(1)使用iconv库来转换编码方式。使用的方法也很简单现在头文件里添加“iconv.h”然后再写一个转换函数,这个函数可以直接复制下面的代码直接用。
string GBKToUTF8(std::string gbkStr, const char* toCode, const char* formCode)
{
iconv_t iconvH;
iconvH = iconv_open(formCode, toCode);
if (iconvH == 0)
{
return 0;
}
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 0;