第一种 编码转换
int CMyUtils::convert(char *from_charset,char *to_charset,const char *inbuf,size_t inlen,char *outbuf,size_t outlen) { iconv_t cd; const char* temp = inbuf; const char** pin = &temp; char** pout = &outbuf; memset(outbuf,outlen); cd = iconv_open(to_charset,from_charset); if(cd == 0) return -1; if(iconv(cd,pin,&inlen,pout,&outlen) == -1) return -1; return 0; } std::string CMyUtils::gb2312ToUtf8(const char *inbuf) { size_t inleng = strlen(inbuf); char* outbuf = new char[inleng * 2 + 2]; std::string strRet; if( convert("gb2312","utf-8",inbuf,inleng,outbuf,inleng * 2 + 2) == 0 ) strRet = outbuf; delete[] outbuf; return strRet; }
头文件要加这个 #include "../external/win32-specific/icon/include/iconv.h"
这种感觉不太好,转来转去感觉不踏实。
第二种 用plist存储字符串,然后再读取,当然要用utf8编码存储
plits:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>data</key> <dict> <key>string1</key> <string>中华好诗词</string> </dict> <key>Metadata</key> <dict> <key>format</key> <integer>1</integer> </dict> </dict> </plist>
然后用的时候:
Configuration *pConf = Configuration::getInstance(); pConf->loadConfigFile("strings.plist"); pConf->getValue("string1").asstring();值得注意的一点是key貌似不能用中文。