cocos2dx libiconv 转码
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 转码的更多相关文章
- Cocos2dx游戏源码合集(BY懒骨头+持续更新+2014.02.21)
转自:http://blog.csdn.net/iamlazybone/article/details/19612941 声明: <萝莉快跑><喵汪大战>两个demo的原作者b ...
- 15款Cocos2d-x游戏源码
(1)用cocos2d-x开发的中国象棋游戏源码 使用Cocos2d-X2.2.3开发的一款中国象棋游戏,游戏中可以实现.新局面.悔棋.游戏音乐.胜利后会显示游戏结果. 源码下载:http://www ...
- cocos2d-x 纹理源码分析
转自:http://blog.csdn.net/honghaier/article/details/8068895 当一张图片被加载到内存后,它是以纹理的形式存在的.纹理是什么东西呢?纹理就是一块内存 ...
- [置顶] Cocos2d-x 实例源码分析之二 小实例的主框架
这篇文章是分析第一个小实例ActionTest的源码.其实所有实例程序的结构都是一样的,只有特定方法里的代码不同,大的框架都是一样的.也就是说看完这篇文章你就可以自己开始分析其他源码了. 废话不多说, ...
- xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误
性能分析:出现如下错误: xcode profile Variable has incomplete type class “CC_DLL” 解决办法:在 xcode的Build Setting ...
- 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码
前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...
- LNMP源码编译安装(centos7+nginx1.9+mysql5.6+php7)
1.准备工作: 1)把所有的软件安装在/Data/apps/,源码包放在/Data/tgz/,数据放在/Data/data,日志文件放在/Data/logs,项目放在/Data/webapps, mk ...
- [cocos2dx] 让UIButton支持disable状态
摘要: 主要解决cocos2dx-2.2.2版本中, UIButton显示不了disable状态图的问题. 顺便, 理解了一下cocos2dx中UIWidget的渲染原理. 博客: http://ww ...
- 利用VS编译libiconv库
参考文章:http://blog.csdn.net/ghevinn/article/details/9834119 关于中文字符编码问题,这篇文章里面讲的很详细-->http://www.tui ...
随机推荐
- 各个城市优步uber注册司机官网地址汇总
uber城市 开通uber城市 开通优步城市 哪些城市开通了uber 哪些城市开通了优步 分类: uber专车资讯 作为专车模式的创立者,Uber公司很早就进入了中国区域.优步在中国市场也是胸怀大 ...
- JQuery插件的学习
此前一直想就关于Jquery插件的开发,做一个深入的学习,由于各种原因,当然主要是自己太懒了...今天就系统分析一下Jquery插件的开发(参考了http://www.xprogrammer.com/ ...
- Unbutu网卡驱动安装(Intel内置网卡8086:15b8)
工作中需要在新的实验平台上安装unbuntu14.04操作系统,系统安装好之后发现无法连接网络,分析后是由于缺少网卡驱动的原因. 下面把分析问题的过程及安装网卡驱动步骤介绍如下: 查看PCI信息 su ...
- HDU ACM 1496 Equations
Equations Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...
- 第二百五十三、四、五天 how can I 坚持
出去玩了几天,好累啊. 周五,坐了半天车.到了西柏坡,下午撕名牌,好疯狂啊,最终还是以有人受伤为代价结束了战斗.晚上吃蛋糕.水饺,还有面条,就是我的奖品没拿到.哎.. 周六,上午滑雪,两年没滑了,都忘 ...
- 有趣的Node爬虫,数据导出成Excel
最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...
- Xshell异常断开
这可能是由于 SSH 超时断开连接 导致的!可以这样做...修改/etc/ssh/sshd_config文件,找到 ClientAliveInterval 0和ClientAliveCountMax ...
- 浅析网站开发中的 meta 标签的作用
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 线程暴长~Quartz中创建Redis频繁后导致线程暴长
在最近项目开发过程中,在进行任务调度处理过程中,出现了一个问题,它的线程数暴长,从20多个可以到1000多个,如果你的服务器性能好的话,可以到10000多个,太恐怖了,就算你的服务再好,早晚有一天也会 ...