cocos2d-x解决中文乱码问题

例子从别人那转的2种方法

#include "iconv/iconv.h" //iconv.lib
#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
 
int 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;
}
/**
**在封装一层,直接传入一个string,转换后还回对应的编码给你
*/
const char* GBKToUTF(std::string &gbkStr)
{
    GBKToUTF8(gbkStr,"gbk","utf-8");
    return gbkStr.c_str();
}
 
#endif
描述: 原理就是将字符串转为 UFT8, C++ 里面默认的字符编码是 GB2312 有图有真相 ,而coco2d-x 支持的编码是 UTF8, 所以放中文会有乱码。好在 C++ 支持标志 L 将字符串编译成 unicode(L"中国"),unicode 转UTF8就方便了,关于这些编码之间的关系,我觉得作为一个合格的程序员必须搞清楚。 libconv.lib

原文:http://my.csdn.net/q3745960/code/detail/30772

=========================================================================================================================

原文:http://www.eoeandroid.com/thread-250673-1-1.html


鉴于网上的一些解方案都不方便(也许我没找到)自己研究了一个方法,
其实原理就是将字符串转为 UFT8,
C++ 里面默认的字符编码是 GB2312 有图有真相
,而coco2d-x 支持的编码是 UTF8, 所以放中文会有乱码。好在 C++ 支持标志 L 将字符串编译成 unicode(L"中国"),unicode 转UTF8就方便了,关于这些编码之间的关系,我觉得作为一个合格的程序员必须搞清楚,自己百度去吧!


下面附上转换方法:(纯 C 做的,我喜欢 C)
  1. //将Unicode小端转为UTF8
  2. int UCS2LEToUTF8(const uint8 *unicode, uint8 *utf8, int size)
  3. {
  4.         int u = 0, i = 0;
  5.         while((unicode[u] || unicode[u+1]) && i > 4)
  6. {
  7.                         utf8[i++] = 0x80 | ((unicode[u+1] & 0x0f) > 6);
  8.                         utf8[i++] = 0x80 | (unicode[u] & 0x3f);
  9.                 u+=2;
  10.         }
  11.         utf8[i] = 0;
  12.         return i;
  13. }
封装方法:
  1. const char *getUTF8_buf(const char *unicode, char *buf, int bufSize)
  2. {
  3.         UCS2LEToUTF8((uint8 *)unicode, (uint8 *)buf, bufSize);
  4.         return (const char*)buf;
  5. }
  6. static int wstrlen(const char *unicode)
  7. {
  8.         int i = 0;
  9.         while(unicode[i] || unicode[i]) // unicode 00 结尾
  10.                 i += 2;
  11.         return i/2;
  12. }
  13. const char *getUTF8(const char *unicode)
  14. {
  15.         int l =  wstrlen(unicode)*3 + 1; //一个unicode字符 转为 UTF8 最多3字节(更多的忽略它)
  16.         uint8 *buf = (uint8 *)malloc(l);
  17.         return getUTF8_buf(unicode, (char*)buf, l);
  18. }

使用方法
  1. label = CCLabelTTF::create(getUTF8_buf((const char*)L"中国", labelBuf, sizeof(labelBuf)), "Marker Felt", 30);

  1. void UILayer::updateScore(int s)
  2. {
  3.         wchar_t buf[128];
  4.         wsprintf(buf, L"位置:%d 米", s);
  5.         //sprintf(labelBuf, "%d", s);
  6.         getUTF8_buf((char*)buf, labelBuf, sizeof(labelBuf));
  7.         label->setString(labelBuf);
  8. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值