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

COCOS2DX 代码中加入中文乱码简单解决办法

时间:2014-09-09 18:39:29      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:des   os   io   ar   for   代码   on   c   amp   

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(‘?‘);  
    }  
}  
std::string WStrToUTF8(const std::wstring& str)  
{  
    std::string result;  
    WStrToUTF8(result, str);  
    return result;

}
std::string timeStr1 = WStrToUTF8(L"%d天%d小时");



COCOS2DX 代码中加入中文乱码简单解决办法

标签:des   os   io   ar   for   代码   on   c   amp   

原文地址:http://my.oschina.net/ffs/blog/311788

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