cocos2dx 3.0 中文 iconv 转换函数

//#include <string> 
#pragma once
#include "cocos2d.h";
#include "iconv\include\iconv.h"
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  // 编译链接的时候指定静态库 
//#pragma comment(lib,"libiconv.lib")  
//#endif  
int 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,0,outlen);
    cd = iconv_open(to_charset,from_charset);
    if(cd==0)
        return -1;
    if(iconv(cd,(char**)pin,&inlen,pout,&outlen) == -1)
        return -1;
    return 0;
}   


std::string tochinese(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;
}

 

posted @ 2014-06-04 17:02  向恺然  阅读(1518)  评论(0编辑  收藏  举报

我必须说的是:我崇尚开源,但鄙视剽窃。本博客所有引用的图片,文章,和代码,均只作为研究学习使用,不作为商业应用。如果我无意中冒犯了您,请发消息留言,我将立即删除。