C0C0S2D-X在windows平台下中文乱码解决;

其实就是将字符串转换成 utf-8; 但在cocos下要判断平台,在安卓平台下,属于正常

下面是 在windwos平台下转换 utf8 的方法 ;;


文章需要用到 iconv的 源文件 和 库文件;;


下载地址 : http://download.csdn.net/detail/hubaolin/5983075


#include "Utils.h"
#include "iconv/iconv.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

int Utils::GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
{
	iconv_t iconvH;
	iconvH = iconv_open(formCode,toCode);
	if(iconvH == 0)
	{
		return -1;
	}
	const char* strChar = gbkStr.c_str();
	const char** pin = &strChar;
	size_t strLength = gbkStr.length();
	char* outbuf = (char*)malloc(strLength*4);
	char* pBuff = outbuf;
	memset(outbuf,0,strLength*4);
	size_t outLength = strLength*4;
	if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))
	{
		iconv_close(iconvH);
		return -1;
	}
	gbkStr = pBuff;
	iconv_close(iconvH);
	return 0;
}


const char* Utils::GBKToUTF(std::string &gbkStr)
{
	this->GBKToUTF8(gbkStr,"gbk","utf-8");
	return gbkStr.c_str();
}

#endif


下面是头文件


#ifndef _UTILS_H_
#define _UTILS_H_


#include "cocos2d.h"

class Utils
{

public:
	//如果是windows平台; 处理Utf-8编码
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);
	const char* GBKToUTF(std::string &gbkStr);
#endif


private:
};



#endif



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值