cocos 中文字符显示问题
2016-03-09 08:52
471 查看
在学习使用cocos中,经常会出现中文字符无法显示问题,在windows上,最简单的就是把带有中文字符的页面保存为utf-8
![](https://img-blog.csdn.net/20160309084318402?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](https://img-blog.csdn.net/20160309084353173?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
编译一下就可以看到中文字符的乱码消失了。
cocos在安卓平台下需要把unicode 转换成utf-8编码,一下提供一个转换函数
class Tools{
static void WStrToUTF8(std::string& dest, const std::wstring& src){
dest.clear();
for (size_t i = 0; i < src.size(); i++){
wchar_t w = src[i];
if (w <= 0x7f){
dest.push_back((char)w);
}
else if (w <= 0x7ff){
dest.push_back(0xc0 | ((w >> 6) & 0x1f));
dest.push_back(0x80 | (w & 0x3f));
}
else if (w <= 0xffff){
dest.push_back(0xe0 | ((w >> 12) & 0x0f));
dest.push_back(0x80 | ((w >> 6) & 0x3f));
dest.push_back(0x80 | (w & 0x3f));
}
else if (sizeof(wchar_t) > 2 && w <= 0x10ffff){
dest.push_back(0xf0 | ((w >> 18) & 0x07)); // wchar_t 4-bytes situation
dest.push_back(0x80 | ((w >> 12) & 0x3f));
dest.push_back(0x80 | ((w >> 6) & 0x3f));
dest.push_back(0x80 | (w & 0x3f));
}
else{
dest.push_back('?');
}
}
}
public:
static std::wstring StringToWString(const std::string &str)
{
wchar_t buf[128];
#ifdef WIN32
setlocale(LC_CTYPE, "");
#endif
mbstowcs(buf, str.c_str(), 10);
std::wstring wstr(buf);
return wstr;
}
static std::string FontToUTF8(const std::wstring& str){
std::string result;
WStrToUTF8(result, str);
return result;
}
static std::string FontToUTF8(const std::string& str){
std::string result;
auto s = StringToWString(str);
WStrToUTF8(result,s );
return result;
}
};
直接在要使用中文字符的地方使用FontToUTF8();
在部分手机上要使用FontToUTF8(L"中文字符");才能显示
编译一下就可以看到中文字符的乱码消失了。
cocos在安卓平台下需要把unicode 转换成utf-8编码,一下提供一个转换函数
class Tools{
static void WStrToUTF8(std::string& dest, const std::wstring& src){
dest.clear();
for (size_t i = 0; i < src.size(); i++){
wchar_t w = src[i];
if (w <= 0x7f){
dest.push_back((char)w);
}
else if (w <= 0x7ff){
dest.push_back(0xc0 | ((w >> 6) & 0x1f));
dest.push_back(0x80 | (w & 0x3f));
}
else if (w <= 0xffff){
dest.push_back(0xe0 | ((w >> 12) & 0x0f));
dest.push_back(0x80 | ((w >> 6) & 0x3f));
dest.push_back(0x80 | (w & 0x3f));
}
else if (sizeof(wchar_t) > 2 && w <= 0x10ffff){
dest.push_back(0xf0 | ((w >> 18) & 0x07)); // wchar_t 4-bytes situation
dest.push_back(0x80 | ((w >> 12) & 0x3f));
dest.push_back(0x80 | ((w >> 6) & 0x3f));
dest.push_back(0x80 | (w & 0x3f));
}
else{
dest.push_back('?');
}
}
}
public:
static std::wstring StringToWString(const std::string &str)
{
wchar_t buf[128];
#ifdef WIN32
setlocale(LC_CTYPE, "");
#endif
mbstowcs(buf, str.c_str(), 10);
std::wstring wstr(buf);
return wstr;
}
static std::string FontToUTF8(const std::wstring& str){
std::string result;
WStrToUTF8(result, str);
return result;
}
static std::string FontToUTF8(const std::string& str){
std::string result;
auto s = StringToWString(str);
WStrToUTF8(result,s );
return result;
}
};
直接在要使用中文字符的地方使用FontToUTF8();
在部分手机上要使用FontToUTF8(L"中文字符");才能显示
相关文章推荐
- cocos2d 场景切换特效
- cocos2dx Action 特效
- cocos2dx Action 动作
- Ubuntu下快速建立跨多个平台的cocos2d-x项目
- cocos2dx 基础
- cocos2d-x之多个移动的小球
- Cocos2d-x之lambda表达式的使用
- [Cocos2d-x v3.x]序列帧动画
- 在mac环境下,导入 quick-cocos2d-x 项目 报错
- Cocos2d-x之多分辨率屏幕适配
- 【cocos2d 3.x】粒子plist的简单使用
- cocos2d-android(1)
- php服务器与cocos2d-x客户端session通信
- Cocos2d-x Android修改ICON名字、更改图标、修改屏幕方向、修改版本号
- cocos2dx TMX地图
- 【原创】cocos2d-x3.9蓝牙开发之蓝牙开启
- cocos2dx android 程序启动黑屏相关
- cocos2dx的jni使用
- cocos2d-x的自学之路0
- COCOS学习笔记--4种Label文字标签及其渲染特性