您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x解决中文乱码问题的几种办法

2013-09-26 11:17 429 查看
将源代码文件保存为utf8编码,不过由于编译器的问题,这种方式会导致很多无法预测的问题

将字符串用utf8编码集中存到一文件中,然后用代码读取这些字符串来使用,这种办法还能很好的支持多语言版本

使用字符串时,先将其转换为utf8编码

我最终使用了第三种方法,第一种撇开不说,第二种实现起来比较麻烦,第三种则要方便很多。

一般在windows上,我们使用API MultiByteToWideChar来进行各种编码转换。

不过这东西只能在Windows上用,在cocos2d-x上用就有点不合时宜的感觉,毕竟安卓上可没这个API。

还好cocos2d-x考虑很周到,它自带了一个iconv库

只需要在项目附加依赖项里加入libiconv.lib,并且包含头文件iconv/iconv.h即可使用。

我通过这个库封装了几个编码转换的函数,代码如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43
#include "Tool.h"

int code_convert(const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen)

{

iconv_t cd;

const char *temp = inbuf;

const char **pin = &temp;

char **pout = &outbuf;

memset(outbuf,0,outlen);

cd = iconv_open(to_charset,from_charset);

if(cd==0) return -1;

if(iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;

iconv_close(cd);

return 0;

}

/*UTF8转为GB2312*/

std::string u2a(const char *inbuf)

{

size_t inlen = strlen(inbuf);

char * outbuf = new char[inlen * 2 + 2];

std::string strRet;

if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)

{

strRet = outbuf;

}

delete [] outbuf;

return strRet;

}

/*GB2312转为UTF8*/

std::string a2u(const char *inbuf)

{

size_t inlen = strlen(inbuf);

char * outbuf = new char[inlen * 2 + 2];

std::string strRet;

if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)

{

strRet = outbuf;

}

delete [] outbuf;

return strRet;

}

然后在每次要使用中文前,用a2u函数将文本转换为utf-8编码,使用例程如下:

1

2

3

4
//刷新分数显示

char buff[1024];

sprintf_s(buff, 1024, "得分:%d", _Score);

_pLabelScore->setString(a2u(buff).c_str());

至此,我们可以在cocos2d-x中正常显示中文了!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: