cocos2dx中使用iconv轉碼(win32,iOS,Android)

來源:互聯網
上載者:User

標籤:

首先貼下環境:Win7 64, NDK r8e, libiconv-1.14, cygwin

一 Win32環境配置

Cocos2D-X內建有win32上的iconv庫,只需要配置一下即可使用。

1 引入標頭檔

屬性->配置屬性->C/C++->附加元件封裝含目錄:

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

2 引入靜態庫:libiconv.lib

屬性->配置屬性->連接器->輸入->附加依賴項:

libiconv.lib

3 定義一個公用轉碼函數
#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);  if(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);  dest_ptr[converted]=‘\0‘;  return true;}
4 測試
void convertTest(){  char inStr[] = "Hello, 這是個測試程式";  char outStr[1024];  iconv_convert(&inStr, sizeof(inStr), "GBK", &outStr, sizeof(outStr), "UTF-8");  CCLog("scr string:%s\n", inStr);  CCLog("dst string:%s\n", outStr);}

輸出結果:

 

to convert 22 characters, 0 mutanted , 29 converted 

scr string:Hello, ????????????

 

5 跨平台標頭檔包含設定

win32按如上方法,設定標頭檔包含路徑和依賴庫,然後才能#include "iconv.h";

iOS上內建libiconv.dylib,只需#include <iconv.h>即可;

Android上需要另外編譯,接下來我們會說;

#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
二 為Android編譯iconv

android下就不能直接使用cocos2d-x提供的iconv庫,需要下載一個已經在linux環境下編譯好的iconv庫。

1 下載

從官網下載libiconv-1.14.tar.gz包,重新命名為libiconv,解壓。

官網:http://www.fsf.org/resources/

2 編譯

開啟cygwin,進入libiconv根目錄,執行:

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

切記此處不是./configure,不然到後面會報一大堆stdio.h裡面的錯。

然後執行:

make

如果你之前make過了,需要先執行:

make clean

然後再執行make

3 拷貝標頭檔

將cocos2dx\platform\third_party\win32下的icov檔案夾拷貝到項目的根目錄下,跟Class同級

4 配置4.1 拷貝libiconv 到工程

將libiconv放置在cocos2d-x-2.1.4根目錄下

4.2 編譯配置

在libiconv目錄下建立一個Android.mk檔案,內容如下:

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 修改項目的Android.mka 添加搜尋路徑

將我們拷貝過來的iconv添加的標頭檔搜尋路徑中

LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/iconv

b 添加iconv_static

LOCAL_WHOLE_STATIC_LIBRARIES += iconv_static

iconv_static是在iconv中的Android.mk中的LOCAL_MODULE的值

c 包含iconv子模組

匯入我們在iconv中定義的Android.mk

$(call import-module,libiconv)

三 常見錯誤1 langinfo.h: No such file or directory

修改libiconv/libcharset/config.h檔案中的宏定義:

#define HAVE_LANGINFO_CODESET 1 

#define HAVE_LANGINFO_CODESET 0

另:切記切記勿用百度,坑死,搜不到想要的;都做程式員了,還是想辦法用Google吧。

2 ‘c‘ undeclared

修改檔案/libcharset/lib/localcharset.c中函數get_charset_aliases (void),

搜尋int c;放到到函數體開頭。

附錄:

其他部落格參考

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中使用iconv轉碼(win32,iOS,Android)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.