标签:
首先贴下环境:Win7 64, NDK r8e, libiconv-1.14, cygwin
Cocos2D-X自带有win32上的iconv库,只需要配置一下即可使用。
属性->配置属性->C/C++->附加包含目录:
$(ProjectDir)..\..\..\..\cocos2dx\platform\third_party\win32\iconv
属性->配置属性->连接器->输入->附加依赖项:
libiconv.lib
#include "iconv.h"
bool iconv_convert(void *src, unsigned int src_len,char *src_charset, void *dest, unsigned int dest_len, char *dest_charset)
{
const char *in;
char *out,*dest_ptr;
size_t in_left,out_left,mutant,converted;
in_left=src_len;
out_left=dest_len;
in=(char *)src;
out=dest_ptr=(char *)dest;
iconv_t oConv=iconv_open(dest_charset,src_charset);
if(oConv==(iconv_t)(-1))
{
CCLog("ERROR: unable to open libiconv.");
return false;
}
mutant = iconv(oConv, &in, &in_left, &out, &out_left );
iconv_close(oConv);
if(mutant == (size_t)(-1))
{
CCLog("ERROR: unable to convert anything.");
return false;
}
converted = dest_len - out_left;
CCLog("to convert %u characters, %u mutanted , %u converted \n",src_len,mutant,converted);
dest_ptr[converted]=‘\0‘;
return true;
}
void convertTest()
{
char inStr[] = "Hello, 这是个测试程序";
char outStr[1024];
iconv_convert(&inStr, sizeof(inStr), "GBK", &outStr, sizeof(outStr), "UTF-8");
CCLog("scr string:%s\n", inStr);
CCLog("dst string:%s\n", outStr);
}
输出结果:
to convert 22 characters, 0 mutanted , 29 converted
scr string:Hello, ????????????
win32按如上方法,设置头文件包含路径和依赖库,然后才能#include "iconv.h";
iOS上自带libiconv.dylib,只需#include <iconv.h>即可;
Android上需要另外编译,接下来我们会说;
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
#include "iconv.h"
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
#include <iconv.h>
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "icov/iconv.h"
#endif
android下就不能直接使用cocos2d-x提供的iconv库,需要下载一个已经在linux环境下编译好的iconv库。
从官网下载libiconv-1.14.tar.gz包,重命名为libiconv,解压。
官网下载地址:http://www.fsf.org/resources/
打开cygwin,进入libiconv根目录,执行:
./configure --build=x86_64-pc-linux-gnu --host=arm-linux-eabi
切记此处不是./configure,不然到后面会报一大堆stdio.h里面的错。
然后执行:
make
如果你之前make过了,需要先执行:
make clean
然后再执行make
将cocos2dx\platform\third_party\win32下的icov文件夹拷贝到项目的根目录下,跟Class同级
将libiconv放置在cocos2d-x-2.1.4根目录下
在libiconv目录下新建一个Android.mk文件,内容如下:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := iconv_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)
将我们拷贝过来的iconv添加的头文件搜索路径中
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes/iconv
LOCAL_WHOLE_STATIC_LIBRARIES += iconv_static
iconv_static是在iconv中的Android.mk中的LOCAL_MODULE的值
导入我们在iconv中定义的Android.mk
$(call import-module,libiconv)
修改libiconv/libcharset/config.h文件中的宏定义:
#define HAVE_LANGINFO_CODESET 1
为
#define HAVE_LANGINFO_CODESET 0
另:切记切记勿用百度,坑死,搜不到想要的;都做程序员了,还是想办法用谷歌吧。
修改文件/libcharset/lib/localcharset.c中函数get_charset_aliases (void),
搜索int c;放到到函数体开头。
其他博客参考
http://blog.csdn.net/smileyuping/article/details/9635365
http://xwrwc.blog.163.com/blog/static/4632000320138151017187/
http://blog.csdn.net/alex_my/article/details/10567541
http://blog.csdn.net/victoryckl/article/details/7089823
cocos2dx中使用iconv转码(win32,iOS,Android)
标签:
原文地址:http://www.cnblogs.com/helinzi/p/5069458.html