码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2dx学习: GBK 和 UTF-8的转换

时间:2015-01-23 14:45:11      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

int code_convert(const char *from_charset, const 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;
    iconv_close(cd);
    return 0;
}

GBK 转 UTF-8

std::string a2u(const char *inbuf)
{
    size_t inlen = strlen(inbuf);
    char * outbuf = new char[inlen * 2 + 2];
    std::string strRet;
    if (code_convert("GBK", "UTF-8", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
    {
        strRet = outbuf;
    }
    delete[] outbuf;
    return strRet;
}
UTF-8转 GBK
std::string u2a(const char *inbuf)
{
    size_t inlen = strlen(inbuf);
    char * outbuf = new char[inlen * 2 + 2];
    std::string strRet;
    if (code_convert("UTF-8", "GBK", inbuf, inlen, outbuf, inlen * 2 + 2) == 0)
    {
        strRet = outbuf;
    }
    delete[] outbuf;
    return strRet;
}



Cocos2dx学习: GBK 和 UTF-8的转换

标签:

原文地址:http://blog.csdn.net/jiuaiwo1314/article/details/43054917

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!