cocos2d-x android 编译 iconv库

参考文章: http://momowing.diandian.com/post/2013-01-16/40047183777
不过我要做点修改,把函数改掉,因为到android中会报错,如下:
conversion from 'char const**' to 'char**' [-fpermissive]。。。。。。。。。。。。。。。。
这个解决方法:
       把有const删了,即可。
我还是具体讲讲 在android上编译iconv吧:


一: 新建一个android项目,并且导入eclipse中(应该都知道);


二: 去网上下载个iconv的库文件:https://dl.dropbox.com/u/99895284/iconv.zip解压,并且得到iconv文件夹,把该文件夹放入cocos2d-x的文件夹下:如图



三: 修改新建的android项目中的jni/Android.mk文件,如图:



//其中 编译的Tools是我新建的类,用于编码转换,也就是用到iconv的一个类。


四: 为了以后编译更加简单,我还把iconv这个文件夹复制到ndk目录下的source文件夹:如图

为什么我放到这里,可以参考我的一篇博客: http://blog.csdn.net/cwn0812/article/details/10741275


五: 配置项目属性:
1): Builders -> new -> Program
2): Main -> Location -> Browse File System -> 选择NDK路径下的 ndk-build.cmd文件,确认
  Main -> Working Directory -> Browse Workspace -> 选择你的项目,确认
3): Refresh -> 打钩Refresh resources upon completion -> 选择Specific resources -> 选择项目的jni文件夹,确定
4): 跳过Environment ,因为我把要编译的文件夹都放到了ndk目录的resource文件夹下了
5): Build Options -> 打钩during a “Clean” ,和Specify working ... 并且选中选择项目的jni文件夹确定,
具体参考上面说的博客,里面有图。


六: 完成。clean 下项目。如图:


七:我封装的类Tools (参考他们写的类,做些修改)

Tools.cpp
int Tools::code_convert( char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen )
{
	iconv_t cd;
	char *temp = inbuf;
	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;
}

std::string Tools::u2a( const char *_inbuf )
{
	char *inbuf = new char[strlen(_inbuf)+1];
	strcpy(inbuf, _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 [] inbuf;
	delete [] outbuf;
	return strRet;
}

std::string Tools::a2u( const char *_inbuf )
{
	char *inbuf = new char[strlen(_inbuf)+1];
	strcpy(inbuf, _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;
	delete [] inbuf;
	return strRet;
}

在HelloWorldScene.cpp中调用
std::string str = Tools::a2u("中文 哈哈wuliao");

		CCLabelTTF* label = CCLabelTTF::create(str.c_str(),"Arial",27);
		label->setPosition(ccp(size.width/2,size.height/2));
		this->addChild(label);

运行到真机,测试成功啦。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值