#include <iconv.h>
static std::string encode_convert(const char* fromcode, const char* tocode, const char* text)
{
char* tmp = (char*)text;
size_t lenSrc = strlen(tmp);
size_t lenDst = lenSrc * 5;
char* out = (char*)malloc(lenDst);
memset(out, 0, lenDst);
char* pFreeOut = out;
iconv_t cd = iconv_open(tocode, fromcode);
size_t ret = iconv(cd, &tmp, &lenSrc, &out, &lenDst);
if (ret == -1)
{
return "";
}
std::string retStr(pFreeOut);
iconv_close(cd);
free(pFreeOut);
return retStr;
}
// GBK转utf-8, SQLITE存储中文时需要, 因为项目的编码方式是UNICODE
static std::string GBK2UTF8(const char* text)
{
if (strlen(text) == 0)
{
return "";
}
return encode_convert("GBK", "UTF-8", text);
}
static std::string UTF82GBK(const char* text)
{
if (strlen(text) == 0)
{
return "";
}
return encode_convert("UTF-8", "GBK", text);
}
static std::string UICODE2GBK(const char* text)
{
return encode_convert("EUC-CN", "GBK", text);
}
iconv 转编码
最新推荐文章于 2024-08-04 20:05:01 发布