Reprinted! Please indicate the source when reprint: http://blog.csdn.net/aa4790139/article/details/8118536
As the problem of Chinese garbled characters was not solved in the previous lecture, we had to study how to solve the problem.
Vs the default source file character set is a multi-byte character set, which is a localized language character set. If you use a Chinese system or simplified Chinese, the default character set is GBK, and the source code does not contain non-ascll codes.
To make it display properly on Win32, You need to convert it into a UTF-8. The following describes how to solve this problem.
Solution 1: Function Conversion Encoding
For future development convenience, a separate class is written ....
Tools. h
#ifndef _TOOLS_H_#define _TOOLS_H_#include "cocos2d.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#include "iconv\iconv.h"int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);#endif#endif
Tools. cpp
# Include "tools. H "# include" iconv \ iconv. H "# If (cc_target_platform = cc_platform_win32) // character conversion enables the cocos2d-x to support Chinese display int gbktoutf8 (STD: string & gbkstr, const char * tocode, const char * formcode) {iconv_t iconvl; iconvl = iconv_open (formcode, tocode); If (iconvl = 0) {return-1;} const char * strchar = gbkstr. c_str (); const char ** pin = & strchar; size_t strlength = gbkstr. length (); char * outbuf = (char *) malloc (strlength * 4); char * pbuff = outbuf; memset (outbuf, 0, strlength * 4 ); size_t outlength = strlength * 4; If (-1 = iconv (iconvl, pin, & strlength, & outbuf, & outlength) {iconv_close (iconvl); Return-1 ;} gbkstr = pbuff; iconv_close (iconvl); Return 0;}/*** In the encapsulation layer, directly input a string, after conversion, return the corresponding encoding */const char * gbktoutf (STD: string & gbkstr) {gbktoutf8 (gbkstr, "GBK", "UTF-8 "); // The default values for the next two parameters are used to avoid the trouble of returning gbkstr when passing the parameters later. c_str () ;}# endif
Haha ~ Now, you only need to use gbktoutf (string & gbkstr). After processing, the corresponding encoding will be returned to you ..
Let's see how to use it...
(Note: Right-click the project and choose Properties> connector> input> Add dependency column> next to a button. Click Open and add libiconv to another line. lib, or add libiconv. lib)
STD: String China = "Chinese! Haha "; # If (cc_target_platform = cc_platform_win32) gbktoutf8 (China," GBK "," UTF-8 "); # endif ccmenuitem * chinaitem = ccmenuitemfont: Create (China. c_str (), this, null); chinaitem-> setposition (CCP (size. width/2, size. height/2); this-> addchild (chinaitem );
Running effect:
Solution 2: Read UTF-8 from external files
We recommend that you use the resource file for configuration saving, such as XML to save the encoding method of its UTF-8, will naturally let me think, japanese and Korean are waiting for the normal display of the languages in various countries. For the sake of internationalization of your software... try this method! Then, based on the language of the mobile phone system, and then dynamically read the resources in your file...
Let's take a look at our XML file:
<Dict> <key> chinese1 </key> <string> good day </string> <key> Japan </key> <string> good day </string> <key> Spanish </key> <string> Buen día </string> </dict> </plist>
Then let's take a look at how to use it:
// Use ccdictionary to read XML ccdictionary * strings = ccdictionary: Create ("fonts/strings. XML "); // Chinese, Japanese, Spanish: objectforkey obtains the corresponding string const char * Chinese = (ccstring *) strings-> objectforkey (" chinese1 ") based on the key ")) -> m_sstring.c_str (); const char * Japanese = (ccstring *) strings-> objectforkey ("Japanese")-> m_sstring.c_str (); const char * Spanish = (ccstring *) strings-> objectforkey ("Spanish")-> m_sstring.c_str (); cclabelbmfont * label1 = cclabelbmfont: Create (Spanish, "fonts/arial-unicode-26.fnt"); addchild (label1); label1-> setposition (CCP (S. width/2, S. height/4*3-20); cclabelbmfont * label2 = cclabelbmfont: Create (Chinese, "fonts/arial-unicode-26.fnt"); addchild (label2 ); label2-> setposition (CCP (S. width/2, S. height/4*2); cclabelbmfont * label3 = cclabelbmfont: Create (Japan, "fonts/arial-unicode-26.fnt"); addchild (label3); label3-> setposition (CCP (S. width/2, S. height/4*1 ));
Running effect:
Haha ~ It is displayed... Thank you for reading my blog, but I didn't see any comments! Hope you can see your footprints!
If the story is incorrect or incorrect, I hope you can point it out!
Source code: http://download.csdn.net/detail/aa4790139/4694562
Reference: http://blog.csdn.net/xiaoxiangp/article/details/7693343