iconv下载(Android)已编译完的iconv包(用这个即可)
ios自带libiconv,只需#include <iconv.h>即可
步骤
1.libiconv解压文件放置
直接将解压的libiconv文件夹放在cocos2dx游戏引擎cocos2d-x-2.1.4目录下
写Android.mk放到libiconv根目录下
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)
蓝色为变化的内容,与1.上面的蓝色对应。
3.iconv.h头文件拷贝
将程序中cocos2dxplatform hird_partywin32目录下的iconv文件夹拷贝到自己工程的Classes目录下
4.程序修改
.h文件
#include <string> #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 #include "iconv/iconv.h" #elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS #include <iconv.h> #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include "iconv/iconv.h" #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());
Demo
CCommonTool.h
#ifndef __CCOMMONTOOL_SCENE_H__ #define __CCOMMONTOOL_SCENE_H__ #include <string> #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 #include "iconv/iconv.h" #elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS #include <iconv.h> #elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include "iconv/iconv.h" #endif class CCommonTool { public: /*---------------------------------------------------*/ // 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 ); }; #endif
CCommonTool.cpp
#include "CommonTool.h" using namespace std; 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; }
TinyXmlDemo.h
#ifndef __TINYXMLDEMO_SCENE_H__ #define __TINYXMLDEMO_SCENE_H__ #include "cocos2d.h" class TinyXmlDemo : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); static void parseTinyXMLFile(); // implement the "static node()" method manually CREATE_FUNC(TinyXmlDemo); }; #endif
TinyXmlDemo.cpp
#include "TinyXmlDemo.h" #include "CommonTool.h" using namespace cocos2d; using namespace tinyxml2; CCScene* TinyXmlDemo::scene() { CCScene * scene = NULL; do { scene = CCScene::create(); CC_BREAK_IF(! scene); TinyXmlDemo *layer = TinyXmlDemo::create(); CC_BREAK_IF(! layer); scene->addChild(layer); } while (0); return scene; } bool TinyXmlDemo::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); TinyXmlDemo::parseTinyXMLFile(); bRet = true; } while (0); return bRet; } void TinyXmlDemo::menuCloseCallback(CCObject* pSender) { } void TinyXmlDemo::parseTinyXMLFile() { char inStr[] = "Hello, 这是个测试程序"; std::string outStr = CCommonTool::a2u(inStr); CCLog("scr string:%s ", inStr); CCLog("dst string:%s ", outStr.c_str()); }
输出: