cocos2dx轉換文字爲utf-8

在cocos2dx中經常需要轉換文字爲utf-8

// 轉成UTF-8
inline const char* G2U(const char* str)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

    int len = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);

    wchar_t* wstr = new wchar_t[len+1];

    memset(wstr,0,len+1);

    MultiByteToWideChar(CP_ACP,0,str,-1,wstr,len);

    len = WideCharToMultiByte(CP_UTF8,0,wstr,-1,NULL,0,NULL,NULL);

    char* temp = new char[len +1];

    memset(temp,0,len+1);

    WideCharToMultiByte(CP_UTF8,0,wstr,-1,temp,len,NULL,NULL);
    if (wstr)
    {
        delete []wstr;
    }
    return temp;
#else
    return str;
#endif
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章