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

Cocos2dx学习: GBK 和 UTF-8的转换,第1张

概述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 =
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,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",inlen * 2 + 2) == 0)    {        strRet = outbuf;    }    delete[] outbuf;    return strRet;}
总结

以上是内存溢出为你收集整理的Cocos2dx学习: GBK 和 UTF-8的转换全部内容,希望文章能够帮你解决Cocos2dx学习: GBK 和 UTF-8的转换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1034907.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存