码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d-x封装一个转码的工具解决中文乱码可以直接拖过去用通用跨平台

时间:2014-08-16 17:09:10      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x   中文乱码   转码   

今天白白给大家分享一个转码的工具函数,如果大家需要在项目中显示中文,可以直接使用,Android和IOS部分都是通用的,不用太麻烦的分平台操作。

转载请注明地址http://blog.csdn.net/u010229677

首先,这个函数是这样的,可以直接拖进去用

<span style="font-size:18px;"><span style="font-size:18px;">#ifndef __TRANSFORMUTF__
#define __TRANSFORMUTF__
#include "../../../win32-specific/icon/include/iconv.h"
#pragma comment(lib,"libiconv.lib")
#include "cocos2d.h"
using namespace std;

//使用方法:string s = GBtoUTF("确定");  或者  <span style="font-family: Arial, Helvetica, sans-serif;">string s = </span><span style="font-family: Arial, Helvetica, sans-serif;">UTFtoGB</span><span style="font-family: Arial, Helvetica, sans-serif;">("确定");</span>

static int 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,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;
}
/*UTF8 To GB2312*/
static string UTFtoGB( const char *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 [] outbuf;
	return strRet;
}
/*GB2312 To UTF8*/ //使用中文时用该函数转换
static string GBtoUTF( const char *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;
	return strRet;
}

#endif</span></span>
好了,这样大家就能直接用了,这里使用到了iconv这个库,
首先我们需要下载一个iconv这个库

然后大家将下载到的iconv库解压至项目根目录。也就是和cocos2dx, extensions这些库的同级目录。

将这个库拖到ios里面,这样ios这边就能直接工作了,

对于windows这边,配置一下库就可以了

Android环境,大家照下面方法做就ok

编写iconv库所需的Android.mk。

     这步要仔细看了,很多文章这步没写清楚,让我走了许多冤枉路。

     进入到刚才解压出的iconv文件夹,用记事本之类的建立一个Android.mk文件,

将下面内容复制到这个文件中,不要写错,很重要的

<span style="font-size:18px;">LOCAL_PATH:= $(call my-dir)
#libiconv.so
include $(CLEAR_VARS)
LOCAL_MODULE := 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)</span>

编写Android项目里面的Android.mk文件,如下图所示

cocos2d-x3.0以前的版本:

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes
LOCAL_C_INCLUDES +=   $(LOCAL_PATH)/../iconv/include   $(LOCAL_PATH)/../libiconv/libcharset   $(LOCAL_PATH)/../libiconv/lib   $(LOCAL_PATH)/../libiconv/libcharset/include   $(LOCAL_PATH)/../libiconv/srclib   $(LOCAL_PATH)/../iconv

  
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += libiconv

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)
$(call import-module,iconv)

cocos2d-x3.0以后的版本:建议把iconv库放到cocos2d文件夹里面,前面操作不便,只是项目的android.mk配置是这样的

LOCAL_C_INCLUDES +=   $(LOCAL_PATH)/../../iconv/include   $(LOCAL_PATH)/../../libiconv/libcharset   $(LOCAL_PATH)/../../libiconv/lib   $(LOCAL_PATH)/../../libiconv/libcharset/include   $(LOCAL_PATH)/../../libiconv/srclib   $(LOCAL_PATH)/../../iconv

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static

# LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static
# LOCAL_WHOLE_STATIC_LIBRARIES += spine_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
 LOCAL_WHOLE_STATIC_LIBRARIES += libiconv

include $(BUILD_SHARED_LIBRARY)

$(call import-module,.)
$(call import-module,audio/android)
$(call import-module,extensions) 
$(call import-module,editor-support/cocostudio)

# $(call import-module,Box2D)
# $(call import-module,editor-support/cocosbuilder)
# $(call import-module,editor-support/spine)
# $(call import-module,editor-support/cocostudio)
# $(call import-module,network)
# $(call import-module,extensions)
 $(call import-module,iconv)


这样我们的工作就开顺利开心的完成了



cocos2d-x封装一个转码的工具解决中文乱码可以直接拖过去用通用跨平台,布布扣,bubuko.com

cocos2d-x封装一个转码的工具解决中文乱码可以直接拖过去用通用跨平台

标签:cocos2d-x   中文乱码   转码   

原文地址:http://blog.csdn.net/u010229677/article/details/38614369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!