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

Source: Internet
Author: User

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

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.

 

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

 

# Ifndef _ TRANSFORMUTF __# define _ TRANSFORMUTF __# include .. /.. /.. /win32-specific/icon/include/iconv. h # pragma comment (lib, libiconv. lib) # include cocos2d. husing namespace std; // usage: string s = GBtoUTF (OK); or string s = UTFtoGB (OK); 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
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

 

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)

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: info-contact@alibabacloud.com 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.