加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 解决中文乱码

发布时间:2020-12-14 17:00:30 所属栏目:百科 来源:网络整理
导读:新建.h文件,加入一下代码 #pragma once #include"string.h" #include"stdlib.h" #ifdef WIN32 #define UTEXT(str) GBKToUTF8(str) #else #define UTEXT(str) str #endif #ifdef WIN32 #include "cocos2dexternalwin32-specificiconincludeiconv.h" sta

新建.h文件,加入一下代码

#pragma once

#include"string.h"
#include"stdlib.h"

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


#ifdef WIN32
#include "cocos2dexternalwin32-specificiconincludeiconv.h"

static char g_GBKConvUTF8Buf[500] = { 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,sizeof(g_GBKConvUTF8Buf));

    char* outbuf = (char*)malloc(outLength);
    char* pBuff = outbuf;
    memset(outbuf,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

在需要转换字符的地方调用 UTEXT() 就行

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读