Cocos2d-x encapsulates a transcoding tool to solve Chinese Garbled text can be dragged directly to use universal cross-platform

Source: Internet
Author: User

TodayIn vainI would like to share with you a transcoding tool function. If you need to display Chinese characters in the project, you can use it directly. Android and IoS are both common and do not need to perform platform-based operations too much.

Reprinted please indicate the address

First, this function is like this and can be dragged in and used directly.

<Span style = "font-size: 18px;"> <span style = "font-size: 18px; "> # ifndef _ transformutf __# DEFINE _ transformutf __# include ".. /.. /.. /win32-specific/icon/include/iconv. H "# pragma comment (Lib," libiconv. lib ") # include" cocos2d. H "using namespace STD; // usage: String S = gbtoutf (" OK "); or <span style =" font-family: Arial, Helvetica, sans-serif; "> string S = </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> utftogb </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> (" OK "); </span> static 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 to gb2312 */static string utftogb (const char * inbuf) {size_t inlen = strlen (inbuf ); char * outbuf = new char [inlen * 2 + 2]; string strret; If (code_convert ("UTF-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) = 0) {strret = outbuf;} Delete [] outbuf; return strret ;} /* gb2312 to utf8 * // use this function to convert static string gbtoutf (const char * inbuf) {size_t inlen = strlen (inbuf ); char * outbuf = new char [inlen * 2 + 2]; string strret = ""; if (code_convert ("gb2312", "UTF-8", inbuf, inlen, outbuf, inlen * 2 + 2) = 0) {strret = outbuf;} Delete [] outbuf; return strret ;}# endif </span>
Okay, so that everyone can use it directly. Here we use the iconv library,
First, we need to download the iconv library.

Then, extract the downloaded iconv Library to the project root directory. That is, the same directory as the cocos2dx and extensions libraries.

Drag this library to iOS so that IOS can work directly,

For Windows, configure the library.

In the android environment, you can do the following:

Android. mk required for compiling the iconv library.

I have read this step carefully. Many articles have not been clearly written in this step, which has left me a lot of mistakes.

Go to the extracted iconv folder and use NotePad to create an android. mk file,

Copy the following content to this file. Do not make any mistakes. It is very important

<span style="font-size:18px;">LOCAL_PATH:= $(call my-dir)#libiconv.soinclude $(CLEAR_VARS)LOCAL_MODULE := libiconvLOCAL_CFLAGS :=   -Wno-multichar   -DAndroid   -DLIBDIR="c"   -DBUILDING_LIBICONV   -DIN_LIBRARYLOCAL_SRC_FILES :=   libcharset/lib/localcharset.c   lib/iconv.c   lib/relocatable.cLOCAL_C_INCLUDES +=   $(LOCAL_PATH)/include   $(LOCAL_PATH)/libcharset   $(LOCAL_PATH)/lib   $(LOCAL_PATH)/libcharset/include   $(LOCAL_PATH)/srclibinclude $(BUILD_STATIC_LIBRARY)</span>

Compile the Android. mk file in the android project, as shown in

Cocos2d-x3.0 versions earlier:

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../ClassesLOCAL_C_INCLUDES +=   $(LOCAL_PATH)/../iconv/include   $(LOCAL_PATH)/../libiconv/libcharset   $(LOCAL_PATH)/../libiconv/lib   $(LOCAL_PATH)/../libiconv/libcharset/include   $(LOCAL_PATH)/../libiconv/srclib   $(LOCAL_PATH)/../iconv  LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_staticLOCAL_WHOLE_STATIC_LIBRARIES += box2d_staticLOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_staticLOCAL_WHOLE_STATIC_LIBRARIES += libiconvinclude $(BUILD_SHARED_LIBRARY)$(call import-module,cocos2dx)$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)$(call import-module,CocosDenshion/android)$(call import-module,extensions)$(call import-module,external/Box2D)$(call import-module,external/chipmunk)$(call import-module,iconv)

Cocos2d-x3.0 version later: it is recommended to put the iconv library in the cocos2d folder, the previous operation is inconvenient, but the project Android. mk configuration is like this

LOCAL_C_INCLUDES +=   $(LOCAL_PATH)/../../iconv/include   $(LOCAL_PATH)/../../libiconv/libcharset   $(LOCAL_PATH)/../../libiconv/lib   $(LOCAL_PATH)/../../libiconv/libcharset/include   $(LOCAL_PATH)/../../libiconv/srclib   $(LOCAL_PATH)/../../iconvLOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static# LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static# LOCAL_WHOLE_STATIC_LIBRARIES += spine_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static LOCAL_WHOLE_STATIC_LIBRARIES += libiconvinclude $(BUILD_SHARED_LIBRARY)$(call import-module,.)$(call import-module,audio/android)$(call import-module,extensions) $(call import-module,editor-support/cocostudio)# $(call import-module,Box2D)# $(call import-module,editor-support/cocosbuilder)# $(call import-module,editor-support/spine)# $(call import-module,editor-support/cocostudio)# $(call import-module,network)# $(call import-module,extensions) $(call import-module,iconv)

In this way, our work will be completed successfully and happily.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.