cocos2dx在写中文字体的时候出现乱码

有时候,根据程序的需要,可能会添加中文字体,但是程序在编码的时候可以能会出现乱码,那么这么处理呢,看如下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#define UTEXT(str) GBKToUTF8(str)
#else
#define UTEXT(str) str
#endif

#ifdef WIN32
#include "platform/third_party/win32/iconv/iconv.h"

static char g_GBKConvUTF8Buf[5000] = {0};
const char* GBKToUTF8(const char *strChar)
{

    iconv_t iconvH;
    iconvH = iconv_open("utf-8","gb2312");
    if (iconvH == 0)
    {
        return NULL;
    }
    size_t strLength = strlen(strChar);
    size_t outLength = strLength<<2;
    size_t copyLength = outLength;
    memset(g_GBKConvUTF8Buf, 0, 5000);

    char* outbuf = (char*) malloc(outLength);
    char* pBuff = outbuf;
    memset( outbuf, 0, outLength);

    if (-1 == iconv(iconvH, &strChar, &strLength, &outbuf, &outLength))
    {
        iconv_close(iconvH);
        return NULL;
    }
    memcpy(g_GBKConvUTF8Buf,pBuff,copyLength);
    free(pBuff);
    iconv_close(iconvH);
    return g_GBKConvUTF8Buf;
}
#endif

上面是汉字转换,有可能出先连接出问题的情况,那是程序所用到的iconv的lib没用添加进去,你只需要在你的工程--属性--连接--输入--添加依赖项--libiconv.lib(添加这个lib文件就可以了)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值