COCOS2DX using Iconv transcoding (win32,ios,android)

Source: Internet
Author: User


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


A WIN32 environment configuration cocos2d-x from the Iconv Library with Win32, only need to be configured to use.
1 Introduction to header file properties, Configuration Properties->c/c++-> additional Include Directories:
$ (ProjectDir): \.. \.. \.. \cocos2dx\platform\third_party\win32\iconv


2 introduction of static libraries: Additional dependencies, input, connector, configuration properties, properties
Libiconv.lib


3 Defining a common transcoding function
#include "iconv.h" bool Iconv_convert (void *src, unsigned int src_len,char *src_charset, void *dest, unsigned int dest_len, Char *dest_charset) {const char *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); oconv== (iconv_t) ( -1)) {Cclog ("error:unable to open Libiconv."); return false;} 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);d est_ptr[converted]= '; return true;}

4 Testing
void Converttest () {char instr[] = "Hello, this is a test program"; Char Outstr[1024];iconv_convert (&instr, sizeof (INSTR), "GBK", & Amp;outstr, sizeof (OUTSTR), "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,????????????
DST String:hello, this is a test program


5 cross-Platform header file contains settings Win32 by the method above, set the header file containing the path and the dependent library, and then 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


Second for Android compiled iconvandroid can not directly use the Cocos2d-x provided Iconv library, need to download a Linux environment has been compiled in the Iconv library.
1 Downloads Download libiconv-1.14.tar.gz package from official website, rename to Libiconv, unzip.
Official website: http://www.fsf.org/resources/


2 Compile 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 files copy the Icov folder under the Cocos2dx\platform\third_party\win32 to the root of the project, similar to 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.clocal_c_includes + =   $ (local_ PATH)/include   $ (local_path)/libcharset   $ (local_path)/lib   $ (local_path)/libcharset/include   $ (Local_path)/srclib  include $ (build_static_library)

4.3 Modifying the Android.mka of a project add a search path we'll copy over the Iconv added header file in the search path
Local_c_includes + = $ (local_path)/. /.. /classes/iconv


b Add iconv_staticlocal_whole_static_libraries + = Iconv_static
Iconv_static is the value of Local_module in Android.mk in Iconv


C contains iconv sub-modules imported into our iconv defined in the ANDROID.MK
$ (call Import-module,libiconv)


Three common errors 1 langinfo.h:no such file or directory modifies the macro definitions in the Libiconv/libcharset/config.h files:
#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 file/libcharset/lib/localcharset.c function get_charset_aliases (void),
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)

Related Article

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.