Cocos2dx学习: GBK 和 UTF-8的转换
2023-06-13 09:16:29 时间
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;
}
相关文章
- C++学习——数据类型(强制)转换详解
- 基于LM331的频率电压转换电路「建议收藏」
- 原码补码反码转换「建议收藏」
- MOS管电平转换电路学习
- linux时间戳转换成时间指令_时间戳转换公式
- 「 [学习记录] JS 类型转换 - 隐式转换 」
- 学习LAMBDA函数:将Excel公式转换为自定义函数(上)
- SQL Server纵表与横表相互转换的方法
- Python将byte数组转换为int详解编程语言
- 【Oracle 转换数字类型的技巧】(oracle转数字类型)
- 快速掌握 Linux 系统 AD 转换技术(ad转换linux)
- Oracle学习| 如何进行列转行操作?一键转换技巧全部揭秘!(列转行oracle)
- 转换mysql语句类型:提高数据库操作效率(mysql语句类型转换)
- 如何将MSSQL数据行转换为列(mssql行转化成列)
- Oracle从中文到英文的转换(oracle 中英文转换)
- npoi2.0将datatable对象转换为excel2007示例