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头文件拷贝

将程序中cocos2dx\platform\third_party\win32目录下的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,,outlen);
cd = iconv_open(to_charset,from_charset);
if(cd==) return -;
if(iconv(cd,pin,&inlen,pout,&outlen)==-) return -;
iconv_close(cd);
return ;
} /*UTF8 To GB2312*/
string CCommonTool::u2a( const char *inbuf )
{
size_t inlen = strlen(inbuf);
char * outbuf = new char[inlen * + ];
string strRet;
if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * + ) == )
{
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 * + ];
string strRet;
if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * + ) == )
{
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,,outlen);
cd = iconv_open(to_charset,from_charset);
if(cd==) return -;
if(iconv(cd,pin,&inlen,pout,&outlen)==-) return -;
iconv_close(cd);
return ;
} /*UTF8 To GB2312*/
string CCommonTool::u2a( const char *inbuf )
{
size_t inlen = strlen(inbuf);
char * outbuf = new char[inlen * + ];
string strRet;
if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * + ) == )
{
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 * + ];
string strRet;
if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * + ) == )
{
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 (); return scene;
} bool TinyXmlDemo::init()
{
bool bRet = false;
do
{ CC_BREAK_IF(! CCLayer::init()); TinyXmlDemo::parseTinyXMLFile(); bRet = true;
} while (); return bRet;
} void TinyXmlDemo::menuCloseCallback(CCObject* pSender)
{ } void TinyXmlDemo::parseTinyXMLFile()
{ char inStr[] = "Hello, 这是个测试程序";
std::string outStr = CCommonTool::a2u(inStr); CCLog("scr string:%s\n", inStr);
CCLog("dst string:%s\n", outStr.c_str()); }

输出:

原文链接:http://www.cnblogs.com/hewei2012/p/3374147.html

cocos2dx libiconv 转码的更多相关文章

  1. Cocos2dx游戏源码合集(BY懒骨头+持续更新+2014.02.21)

    转自:http://blog.csdn.net/iamlazybone/article/details/19612941 声明: <萝莉快跑><喵汪大战>两个demo的原作者b ...

  2. 15款Cocos2d-x游戏源码

    (1)用cocos2d-x开发的中国象棋游戏源码 使用Cocos2d-X2.2.3开发的一款中国象棋游戏,游戏中可以实现.新局面.悔棋.游戏音乐.胜利后会显示游戏结果. 源码下载:http://www ...

  3. cocos2d-x 纹理源码分析

    转自:http://blog.csdn.net/honghaier/article/details/8068895 当一张图片被加载到内存后,它是以纹理的形式存在的.纹理是什么东西呢?纹理就是一块内存 ...

  4. [置顶] Cocos2d-x 实例源码分析之二 小实例的主框架

    这篇文章是分析第一个小实例ActionTest的源码.其实所有实例程序的结构都是一样的,只有特定方法里的代码不同,大的框架都是一样的.也就是说看完这篇文章你就可以自己开始分析其他源码了. 废话不多说, ...

  5. xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误

    性能分析:出现如下错误: xcode profile  Variable has incomplete type   class “CC_DLL” 解决办法:在 xcode的Build Setting ...

  6. 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码

    前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...

  7. LNMP源码编译安装(centos7+nginx1.9+mysql5.6+php7)

    1.准备工作: 1)把所有的软件安装在/Data/apps/,源码包放在/Data/tgz/,数据放在/Data/data,日志文件放在/Data/logs,项目放在/Data/webapps, mk ...

  8. [cocos2dx] 让UIButton支持disable状态

    摘要: 主要解决cocos2dx-2.2.2版本中, UIButton显示不了disable状态图的问题. 顺便, 理解了一下cocos2dx中UIWidget的渲染原理. 博客: http://ww ...

  9. 利用VS编译libiconv库

    参考文章:http://blog.csdn.net/ghevinn/article/details/9834119 关于中文字符编码问题,这篇文章里面讲的很详细-->http://www.tui ...

随机推荐

  1. jQuery遍历Table tr td td中包含标签

    function shengchen() { var arrTR = $("#tbModule").children(); var Context=""; $( ...

  2. SCAU 07校赛 10317 Fans of Footbal Teams

    10317 Fans of Footbal Teams 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description Two famous footba ...

  3. 第二百九十七天 how can I 坚持

    算是在家宅了一天吧,下午睡了会觉,晚上一起做了个饭,中午一起吃的炒菜和徐斌他同学. 还是那么冷啊. 整天都是东扯西扯的. 睡觉. 忘了件重要的事,就是今天第一次喝鸡尾酒,还有,常人之所以是常人,不是因 ...

  4. LINUX如何查看其他用户的操作

    我们知道可以使用history命令,查看自己的操作记录,但如果你是root用户,如何查看其它用户的操作记录呢?   其实history命令只是把当前用户目录下的~/.bash_History文件内容列 ...

  5. CodeForces 489A SwapSort (选择排序法)

    SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...

  6. Umbraco 上传文件到另一个文件夹,而不是media files

    If you want to upload there media files to another place in the same instance of IIS, for example a ...

  7. 【数据库】MySql常用函数梳理

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5866388.html MySql常用函数梳理: 1:数学函数 使用格式:一般用于插入.修改语句中,直接 函数( ...

  8. -webkit-appearance: none;去处select默认小箭头样式

    Html <select class="sel_house_type"> <option value="0">请选择</optio ...

  9. 转载“启动\关闭Oracle数据库的多种方法”--来自百度#Oracle

    启动\关闭Oracle数据库的多种方法 启动和关闭oracle有很多种方法. 这里只给出3种方法: l         Sql*plus l         OEM控制台 l         Wind ...

  10. 允许ubuntu下mysql远程连接

    第一步: gedit /etc/mysql/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0.0.1 或者改为: bind- ...