search
HomeDatabaseMysql TutorialCocos2d-x 对于中文的支持-----iconv库

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: 链接无法直接点击下载的话,请使用右键->迅雷下载

Statement
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools