int Tools::code_convert( char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen ) { iconv_t cd; char *temp = inbuf; char **pin = &temp; char **pout = &outbuf; memset(outbuf,0,outlen); cd = iconv_open(to_charset,from_charset); if(cd==0) return -1; if(iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1; iconv_close(cd); return 0; } std::string Tools::u2a( const char *_inbuf ) { char *inbuf = new char[strlen(_inbuf)+1]; strcpy(inbuf, _inbuf); size_t inlen = strlen(inbuf); char * outbuf = new char[inlen * 2 + 2]; string strRet; if(code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, inlen * 2 + 2) == 0) { strRet = outbuf; } delete [] inbuf; delete [] outbuf; return strRet; } std::string Tools::a2u( const char *_inbuf ) { char *inbuf = new char[strlen(_inbuf)+1]; strcpy(inbuf, _inbuf); size_t inlen = strlen(inbuf); char * outbuf = new char[inlen * 2 + 2]; string strRet; if(code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0) { strRet = outbuf; } delete [] outbuf; delete [] inbuf; return strRet; }
std::string str = Tools::a2u("中文 哈哈wuliao"); CCLabelTTF* label = CCLabelTTF::create(str.c_str(),"Arial",27); label->setPosition(ccp(size.width/2,size.height/2)); this->addChild(label);