cocos2d_x iconv转码

iconv下载(Android)

源码下载地址,已编译完的iconv包(用这个即可)

ios自带libiconv.dylib,只需#include <iconv.h>即可 

步骤

1.libiconv解压文件放置

直接将解压的libiconv文件夹放在cocos2dx游戏引擎cocos2d-2.1rc0-x-2.1.2目录下

libiconv中Andriod.mk文件内容为:

LOCAL_PATH:= $(call my-dir)
#libiconv.so
include $(CLEAR_VARS)
LOCAL_MODULE := libiconv_static

LOCAL_MODULE_FILENAME := libiconv
LOCAL_CFLAGS := \
  -Wno-multichar \
  -DAndroid \
  -DLIBDIR="c" \
  -DBUILDING_LIBICONV \
  -DIN_LIBRARY
LOCAL_SRC_FILES := \
  libcharset/lib/localcharset.c \
  lib/iconv.c \
  lib/relocatable.c
LOCAL_C_INCLUDES += \
  $(LOCAL_PATH)/include \
  $(LOCAL_PATH)/libcharset \
  $(LOCAL_PATH)/lib \
  $(LOCAL_PATH)/libcharset/include \
  $(LOCAL_PATH)/srclib
include $(BUILD_STATIC_LIBRARY)

 

2.修改自己工程中的Andriod.mk文件

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static libiconv_static
            
include $(BUILD_SHARED_LIBRARY)

$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
$(call import-module,extensions) \
$(call import-module,libiconv)

蓝色为变化的内容

 

3.iconv.h头文件拷贝

将程序中cocos2dx\platform\third_party\win32目录下的iconv文件夹拷贝到自己工程的Classes目录下

 

4.程序修改

.h文件

#include "iconv/iconv.h"
 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) 
#pragma comment(lib,"libiconv.lib") 
 #endif

/*---------------------------------------------------*/
// iconv转码
/*---------------------------------------------------*/
static int code_convert( const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen );
static std::string u2a( const char *inbuf );
static std::string a2u( const char *inbuf );

.cpp文件

 int CCommonTool::code_convert( const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf, size_t outlen )
 {
     iconv_t cd;
     const char *temp = inbuf;
     const char **pin = &temp;
     char **pout = &outbuf;
     memset(outbuf,0,outlen);
     cd = iconv_open(to_charset,from_charset);
     if(cd==0) return -1;
     if(iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
     iconv_close(cd);
     return 0;
 }

 /*UTF8 To GB2312*/
 string CCommonTool::u2a( const char *inbuf )
 {
     size_t inlen = strlen(inbuf);
     char * outbuf = new char[inlen * 2 + 2];
     string strRet;
     if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
     {
         strRet = outbuf;
     }
     delete [] outbuf;
     return strRet;
 }

 /*GB2312 To UTF8*/
 string CCommonTool::a2u( const char *inbuf )
 {
     size_t inlen = strlen(inbuf);
     char * outbuf = new char[inlen * 2 + 2];
     string strRet;
     if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
     {
         strRet = outbuf;
     }
     delete [] outbuf;
     return strRet;
 }

程序使用

CCString ccStr;
ccStr.m_sString = CCommonTool::a2u(ccStr.m_sString.c_str());

 

 希望能给你帮助~

posted @ 2014-11-03 10:13  滴水瓦  阅读(417)  评论(0编辑  收藏  举报