cocos2dx windows下使用中文字符串乱码解决方案

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

第一种 编码转换

  1. int CMyUtils::convert(char *from_charset,char *to_charset,const char *inbuf,size_t inlen,char *outbuf,size_t outlen)
  2. {
  3. iconv_t cd;
  4. const char* temp = inbuf;
  5. const char** pin = &temp;
  6. char** pout = &outbuf;
  7. memset(outbuf,outlen);
  8. cd = iconv_open(to_charset,from_charset);
  9. if(cd == 0)
  10. return -1;
  11. if(iconv(cd,pin,&inlen,pout,&outlen) == -1)
  12. return -1;
  13. return 0;
  14. }
  15.  
  16. std::string CMyUtils::gb2312ToUtf8(const char *inbuf)
  17. {
  18. size_t inleng = strlen(inbuf);
  19. char* outbuf = new char[inleng * 2 + 2];
  20. std::string strRet;
  21. if( convert("gb2312","utf-8",inbuf,inleng,outbuf,inleng * 2 + 2) == 0 )
  22. strRet = outbuf;
  23. delete[] outbuf;
  24. return strRet;
  25. }
  1.  

文件要加这个 #include "../external/win32-specific/icon/include/iconv.h"

这种感觉不太好,转来转去感觉不踏实。

第二种 用plist存储字符串,然后再读取,当然要用utf8编码存储

plits:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>data</key>
  6. <dict>
  7. <key>string1</key>
  8. <string>中华好诗词</string>
  9. </dict>
  10. <key>Metadata</key>
  11. <dict>
  12. <key>format</key>
  13. <integer>1</integer>
  14. </dict>
  15. </dict>
  16. </plist>

然后用的时候:
  1. Configuration *pConf = Configuration::getInstance();
  2. pConf->loadConfigFile("strings.plist");
  3. pConf->getValue("string1").asString();
值得注意的一点是key貌似不能用中文

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