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. You can use it only if you need to configure it.
1 Introduction to header file properties, Configuration Properties->c/c++-> Additional include folder:
$ (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 test
void Converttest () {char instr[] = "Hello, this is a test procedure"; 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 procedure.


5 cross-platform header files include settings Win32 as above, set header files including paths and dependent libraries. Then talent include "Iconv.h";
Bring your own libiconv.dylib on iOS, just need to include <iconv.h>.
Android needs to be compiled separately, and then 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 by the Iconv library, you need to download a Linux environment has been compiled in the Iconv library.
1 Downloads Download the libiconv-1.14.tar.gz package from the official website. Rename to Libiconv, unzip.
Official website: http://www.fsf.org/resources/


2 compile to open Cygwin. Go to the Libiconv root folder and run:
./configure--build=x86_64-pc-linux-gnu--host=arm-linux-eabi

Remember this is not a./configure. Otherwise there will be a lot of stdio.h in the back.


Then run:
Make

Let's say you've done it before. Need to run first:
Make clean

And then run make

3 Copy header files copy the Icov folder under Cocos2dx\platform\third_party\win32 to the root folder of the project. Similar to class

4 Configuring 4.1 Copy Libiconv to Project

Place the Libiconv under the cocos2d-x-2.1.4 root folder


4.2 Compiling the configuration

Create a new Android.mk file under the Libiconv folder, such as the following:

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 Change the Android.mka of the project add the search path to the header file that we copied to the Iconv in the search path.
Local_c_includes + = $ (local_path)/. /.. /classes/iconv


b Join Iconv_staticlocal_whole_static_libraries + = Iconv_static
Iconv_static is the value of Local_module in Android.mk in Iconv


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


Three common errors 1 langinfo.h:no such file or directory changes in the macro definition in 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. Can't find what you're looking for. All do the program ape, or think of ways to use Google it.


2 ' C ' Undeclared change 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)

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.