COCOS2DX using Iconv transcoding (win32,ios,android)

Source: Internet
Author: User

First put the environment: Win7, NDK r8e, libiconv-1.14, Cygwin

One WIN32 environment configuration

The cocos2d-x comes with a iconv library on the Win32 and can be used with just a few configurations.

1 Introducing Header Files

Properties, configuration Properties->c/c++-> additional Include Directories:

$ (ProjectDir): \.. \.. \.. \cocos2dx\platform\third_party\win32\iconv

2 Introducing a static library: Libiconv.lib

Additional dependencies, input, connector, configuration properties, properties

Libiconv.lib

3 Defining a common transcoding function
#include "Iconv.h"BOOL Iconv_convert (void *src, unsignedint Src_len,Char *src_charset,void *dest, unsignedint Dest_len,Char *dest_charset) { Constchar *In char *Out,*dest_ptr;size_t in_left,out_left,mutant,converted;In_left=src_len;Out_left=dest_len; In= (char *) src; Out=dest_ptr= (char *) dest;iconv_t Oconv=iconv_open (Dest_charset,src_charset); if (oconv== (iconv_t) (-1)){ Cclog ("Error:unable to open Libiconv.");  ReturnFalse}Mutant = Iconv (Oconv, &In , &in_left, & Out, &out_left); Iconv_close (OCONV);  if (mutant = = (size_t) (-1)) { cclog ("error:unable to convert anything.");   return false; } converted = Dest_len-out_left; Cclog ("to convert%u characters,%u mutanted,%u converted \ n", src_len,mutant,converted); dest_ptr[conve rted]=' + ';  return true;}                 
4 Testing
void Converttest () {  "Hello, this is a test program";   Char outstr[1024x768];" UTF-8 "); Cclog ("SCR string:%s\n", inStr);  Cclog ("DST string:%s\n", outstr);}        

Output Result:

To convert characters, 0 mutanted, converted

SCR String:hello,????????????

5 cross-Platform header file contains settings

Win32 the header file contains a path and a dependent library, as shown above, before it can include "iconv.h";

Bring your own libiconv.dylib on iOS, just include <iconv.h>.

Android needs to be compiled separately, next we will say;

#If Cc_target_platform = = Cc_platform_win32#include "iconv.h"#elif cc_target_platform = = Cc_platform_ IOS#include <iconv.h>#elif cc_target_platform = = cc_platform_android#include "icov/iconv.h" #endif         
Two for Android compiler Iconv

Android cannot directly use the Iconv library provided by cocos2d-x and needs to download a iconv library that has been compiled in a Linux environment.

1 Downloads

Download libiconv-1.14.tar.gz package from official website, rename to Libiconv, unzip.

Official website: http://www.fsf.org/resources/

2 compiling

Open Cygwin, go to libiconv root directory, execute:

. /configure--build=x86_64-pc-linux-gnu--host=arm-linux-eabi

Remember that this is not a./configure, otherwise there will be a lot of stdio.h in the back of the wrong.

Then execute:

Make

If you've done this before, you need to do it first:

Make clean

Then execute make

3 Copy header File

Copy the Icov folder under Cocos2dx\platform\third_party\win32 to the root directory of the project, as Class

4 Configuration 4.1 copy Libiconv to Project

Place the Libiconv in the cocos2d-x-2.1.4 root directory

4.2 Compiling the configuration

Create a new Android.mk file under the Libiconv directory with the following contents:

Local_path:=$ (call My-dir)Include$(Clear_vars)Local_module: = Iconv_staticLocal_module_filename: = LibiconvLocal_cflags:= -Wno-multichar-dandroid-dlibdir= "C"- dbuilding_libiconv-din_librarylocal_src_ FILES := libcharset/lib/localcharset.c lib/iconv.c lib/relocatable.c Local_c_includes + = $ (local_path)/include span class= "variable" >$ (local_path)/libcharset $ ( local_path)/lib $ (local_path)/libcharset/< span class= "keyword" >include $ (local_path)/srclib include $ (build_static_library)   
4.3 Modifying the Android.mka of a project add a search path

We'll copy the Iconv. Added header files in the search path

Local_c_includes + = $ (local_path)/. /.. /classes/iconv

b Add iconv_static

Local_whole_static_libraries + = Iconv_static

Iconv_static is the value of Local_module in Android.mk in Iconv

C contains iconv sub-modules

Import the android.mk that we defined in Iconv

$ (call Import-module,libiconv)

Three common errors 1 langinfo.h:no such file or directory

To modify a macro definition in a libiconv/libcharset/config.h file:

#define HAVE_LANGINFO_CODESET 1

For

#define HAVE_LANGINFO_CODESET 0

Another: Remember to remember not to use Baidu, pit dead, search not to want, do programmers, or think of ways to use Google it.

2 ' C ' undeclared

Modify the function get_charset_aliases (void) in file/libcharset/lib/localcharset.c,

search int C; Put to the beginning of the function body.

Appendix:

Other blog references

http://blog.csdn.net/smileyuping/article/details/9635365

http://xwrwc.blog.163.com/blog/static/4632000320138151017187/

http://blog.csdn.net/alex_my/article/details/10567541

http://blog.csdn.net/victoryckl/article/details/7089823

COCOS2DX using Iconv transcoding (win32,ios,android)

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.