Home > Database > Mysql Tutorial > body text

Cocos2d-x 对于中文的支持-----iconv库

WBOY
Release: 2016-06-07 15:19:16
Original
1324 people have browsed it

http://momowing.diandian.com/post/2013-01-16/40047183777 Jetion: 我们在win32下创建类的时候,文件都是默认的ANSI的式,如果你使用了中文,那么你在win32下就会看到乱码。当然,移植Android的时候,你把文件式改为UTF-8即可。 但是为了方便win32下的调试

http://momowing.diandian.com/post/2013-01-16/40047183777

Jetion:

我们在win32下创建类的时候,文件都是默认的ANSI的格式,如果你使用了中文,那么你在win32下就会看到乱码。当然,移植Android的时候,你把文件格式改为UTF-8即可。
但是为了方便win32下的调试,也为了省事点,我们需要一种方法能直接在win32和Android下显示中文的,这时候我们就需要用到iconv库的,cocos2d-x自带第三方库iconv。

在需要用到的地方,我们只要这么声明就可以:

1

2

#include "platform\third_party\win32\iconv\iconv.h"

#pragma comment(lib,"libiconv.lib")

或者在VS中这么处理:
Cocos2d-x 对于中文的支持-----iconv库
当然还是得加上

1

#include "platform\third_party\win32\iconv\iconv.h"

这样子你就能引用cocos2d-x中自带的iconv库了。
但是当你移植到Android的时候,也许你正在使用cygwin编译,然后发现它报了:
convert_open等方法没找到的错误,因为Android手机上面是不带这个库的,所以你没法找到相关的方法。
这时候我能想到的就是自己去搞一个iconv库来编译到Android上面使用了。
首先:你要去网上下一个iconv库,这边我给出我用的iconv库,虽然不能保证都能用,但是至少我在Android2.3上面是没问题的。下载链接
或许你也可以在网上找到合适的iconv库。
其次:把iconv库解压放到cocos2d-x的根目录下
然后:修改你Android的mk。这里也给出mk的添加代码,如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../HelloWorld/proj.win32 \

$(LOCAL_PATH)/../../../HelloWorld/Classes  \

 $(LOCAL_PATH)/../../../iconv \

            $(LOCAL_PATH)/../../../iconv/include \

            $(LOCAL_PATH)/../../../iconv/libcharset \

            $(LOCAL_PATH)/../../../iconv/libcharset/lib \

            $(LOCAL_PATH)/../../../iconv/libcharset/include                

              

LOCAL_WHOLE_STATIC_LIBRARIES := iconv cocos2dx_static cocosdenshion_static cocos_extension_static

                          

include $(BUILD_SHARED_LIBRARY)

              

$(call import-module,CocosDenshion/android) \

$(call import-module,cocos2dx) \

$(call import-module,extensions) \

$(call import-module,iconv)

最后:给出转换为UTF-8的格式的代码,如下:

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

intDataController::code_convert( constchar *from_charset, constchar *to_charset, constchar *inbuf, size_tinlen, char*outbuf, size_toutlen )

{

    iconv_t cd;

    constchar *temp = inbuf;

    constchar **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);

    return0;

}

/*UTF8 To GB2312*/

string DataController::u2a( constchar *inbuf )

{

    size_tinlen = strlen(inbuf);

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

    string strRet;

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

    {

        strRet = outbuf;

    }

    delete[] outbuf;

    returnstrRet;

}

/*GB2312 To UTF8*/ //使用中文时用该函数转换

string DataController::a2u( constchar *inbuf )

{

    size_tinlen = strlen(inbuf);

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

    string strRet;

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

    {

        strRet = outbuf;

    }

    delete[] outbuf;

    returnstrRet;

}

 

PS: 链接无法直接点击下载的话,请使用右键->迅雷下载

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template