cocos2dx 解决中文乱码

前端之家收集整理的这篇文章主要介绍了cocos2dx 解决中文乱码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

新建.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 "cocos2d\external\win32-specific\icon\include\iconv.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() 就行

原文链接:https://www.f2er.com/cocos2dx/339878.html

猜你在找的Cocos2d-x相关文章