歡迎轉載!轉載時請註明出處:http://blog.csdn.net/aa4790139/article/details/8118536
上一講由於沒有解決中文亂碼的問題,所以不得不研究下如何解決這個問題。
vs默認源文件的字符集是多字節字符集,既本地化語言字符集,如果你用的系統是中文系統,簡體中文,默認字符集是GBK,源碼是不包含非ASCLL碼。
要讓其在win32上正常顯示,就需要將其轉成UTF-8。下面就和大家講解解決這個問題方法。
解決方法一:函數轉換編碼
由於爲了以後開發方便,我一個單獨的類將其寫成了....
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)
- //字符轉換,使cocos2d-x在win32平臺支持中文顯示
- int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
- {
- iconv_t iconvH;
- iconvH = iconv_open(formCode,toCode);
- if(iconvH == 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(iconvH,pin,&strLength,&outbuf,&outLength))
- {
- iconv_close(iconvH);
- return -1;
- }
- gbkStr = pBuff;
- iconv_close(iconvH);
- return 0;
- }
- /**
- **在封裝一層,直接傳入一個string,轉換後還回對應的編碼給你
- */
- const char* GBKToUTF(std::string &gbkStr)
- {
- GBKToUTF8(gbkStr,"gbk","utf-8"); //後面兩個參數就默認了,免得後面再傳參麻煩
- return gbkStr.c_str();
- }
- #endif
呵呵~ 現在就只要使用GBKToUTF(string &gbkstr),處理後返回對應的編碼給你了..
我們再來看怎麼使用的...
(注意:對工程右鍵-> 屬性 -> 連接器 -> 輸入 -> 附加依賴項 欄目->後面有個按鈕,點擊打開,換一行加入libiconv.lib,或者在最後空一格加上libiconv.lib也行)
- std::string china="中文!哈哈";
- #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);
運行效果:
解決方法二:從外部文件讀取UTF-8
推薦大家使用資源文件進行配置保存,如xml將其採用的UTF-8的編碼方式保存,自然會讓我想到,日文、韓文等待各種國家的語言都可以正常顯示了,爲了你的軟件國際化...儘量採用這種方式吧!到時候根據手機系統的語言,然後動態的來讀取你文件中的資源...
先看下我們的xml文件:
- <dict>
- <key>chinese1</key>
- <string>美好的一天</string>
- <key>japanese</key>
- <string>良い一日を</string>
- <key>spanish</key>
- <string>Buen día</string>
- </dict>
- </plist>
然後再來看如何使用:
- //利用CCDictionary來讀取xml
- CCDictionary *strings = CCDictionary::create("fonts/strings.xml");
- //中文,日語,西班牙語:objectForKey根據key,獲取對應的string
- const char *chinese = ((CCString*)strings->objectForKey("chinese1"))->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(japanese, "fonts/arial-unicode-26.fnt");
- addChild(label3);
- label3->setPosition(ccp(s.width/2, s.height/4*1));
運行效果:
呵呵~ 顯示出來了.... 在此感謝大家閱覽我的博文,只是沒有看到大家的留言啊!希望也能看到大家的腳印...呵呵!
如果講述得有誤,或者不對的地方,還望各位指出!
源碼下載地址:http://download.csdn.net/detail/aa4790139/4694562
參考文章:http://blog.csdn.net/xiaoxiangp/article/details/7693343