Cocos2d-x Study Notes (eight) encoding conversion using NDK's own iconv

Source: Internet
Author: User

Cocos2d-x Display Chinese garbled the most commonly used solution is to read XML files, using the Iconv library. Because in the use of Iconv library there are a lot of problems, here to make a summary, to Yimeimei less go some detours.

I use the Cocos2d-x 3.4 version, I also refer to the online tutorial, but there may be some details of the tutorial is not mentioned, here is detailed list.

1. Create a new ChineseString.h file on the cocos2d-x side

The code is as follows:

#ifndef  __ChineseString_H__#define __ChineseString_H__#if  (cc_target_platform == cc_ platform_android) #include   ". /.. /.. /software/android-ndk-r10e/sources/android/support/include/iconv.h "#endif #if  (CC_TARGET_PLATFORM == &NBSP;CC_PLATFORM_WIN32) #include   "External/win32-specific/icon/include/iconv.h" #endif #include < string> #pragma  comment (lib,  "Libiconv.lib") static char g_gbkconvutf8buf[5000] =  {0};class chinesestring{public:static const char* gbktoutf8 (Char *strChar) { Iconv_t  iconvh;iconvh = iconv_open ("Utf-8",  "gb2312"); if (iconvh == 0) { Return null;} Size_t strlength = strlen (Strchar);size_t outlength = strlength * 4; Size_t copylength = outlength;memset (g_gbkconvutf8buf, 0, 5000); char* outbuf  =  (char*) malloc (outlength); Char* pbuff = outbuf;memset (ouTbuf, 0, outlength);if  ( -1 == iconv (Iconvh, &strchar, &strlength,  &outbuf, &outlength))   {  iconv_close (ICONVH);  return  null;  }  memcpy (g_gbkconvutf8buf,pbuff,copylength);   free (PBuff);   Iconv_close (ICONVH);  return g_gbkconvutf8buf;}}; #endif


2. Android Terminal

Online there is a way to download Iconv library, I think that kind of method is very troublesome, and I followed the steps to try the next, no success, back to give up, and finally found a way, that is, the following to introduce the Iconv library with the NDK.

I'm using android-ndk-r10e, and I can see a iconv.h file, which is the path referenced in the cocos2d-x-side code above.

#if (Cc_target_platform = = cc_platform_android) #include ". /.. /.. /software/android-ndk-r10e/sources/android/support/include/iconv.h "#endif

This adjustment is based on the path that the NDK stores.

Then modify the Android.mk file in the Proj.android/jni directory

The main added

Local_static_libraries + = Android_support
$ (call Import-module, Android/support)

Add these two pieces of code to the appropriate place

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/87/3B/wKioL1fYqWqR0SYJAABY88e_luE261.png-wh_500x0-wm_3 -wmp_4-s_2332078002.png "title=" Qq20160914093522.png "alt=" Wkiol1fyqwqr0syjaaby88e_lue261.png-wh_50 "/>

By doing these steps, it is normal to pass.


Note the following points:

1, Android compilation encountered the following error

Invalid conversion from ' const char** ' to ' char** '

This is because when we define GBKToUTF8 this function, the parameters use the const char* Strchar, and then pass in the Iconv function when the NDK iconv.h defines size_t iconv (iconv_t, char**, size_t*, char**, size_t*); So it's OK to just remove the const.

2, the Internet has introduced the NDK R9 iconv.h Code has a problem, need to modify the following, we pay attention to the line, R10 is not the problem

Change #ifdef __cplus_plus//To #ifdef __cplusplus}//extern "C" #endif





Cocos2d-x Study Notes (eight) encoding conversion using NDK's own iconv

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.