参考文章: 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文件,如图:
四: 为了以后编译更加简单,我还把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);