Cocos2d-x Program Windows VC Chinese garbled solution (with MultiByteToWideChar to convert, VC2010 have very strong execution_character_set)

Source: Internet
Author: User
Tags utext

Cocos2d-x Default string constant encoding is UTF8, and the VC in Windows is the same as the system, such as Windows is GB2312 or GBK. Traditional is BIG5 code.
And most of us Chinese compiled with VC string constants are GBK encoded strings.
When drawing on the Cocos2d-x interface It is not known, only think this is the UTF8 string, the result is garbled.
The solution is also very simple. Find the header file add the following code

#ifdef WIN32inline std::wstring AnsiToUnicode(const char* buf){    int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);    if (len == 0) return L"";    std::wstring unicode;    unicode.resize(len);    ::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);    return unicode;}inline std::string UnicodeToUtf8(const wchar_t* buf){    int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);    if (len == 0) return "";     std::string utf8;    utf8.resize(len);    ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);    return utf8;}inlinestd::string AnsioUTF8(const char *strChar){    return UnicodeToUtf8(AnsiToUnicode(strChar).c_str());}//Windows中做编码转换,#define UTEXT(str) AnsioUTF8(str)#else//Android/iOS中不处理.#define UTEXT(str) str#endif

When you need to use the Chinese string

CCLOG(UTEXT("这是中文字符串!"));Text* txt = Text::create();txt->setString(UTEXT("文本中的中文字符正常的!"));

Added, two days after this blog post was published, there was a more straightforward approach.

#ifdef WIN32#pragma execution_character_set("utf-8")#endif

The VC compiler will then encode the string constants as UTF8.

http://www.raysoftware.cn/?p=535

Cocos2d-x program Windows VC Chinese garbled solution (converted with MultiByteToWideChar, VC2010 have very strong execution_character_set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.