C++中文字符编码(GBK/Unicode/UTF8)介绍以及转换(2/2)

上篇主要介绍了3中字符编码的历史、具体编码规则以及相互关联关系,下面介绍如何进行转换。

下面这个函数实现的是GBK与UTF8之间的转换,支持Windows、Linux平台。

#define ENCODING_GBK 0         // Same with CP_ACP(0), default to ANSI code page
#define ENCODING_UTF8 65001    // Same with CP_UTF8(65001), UTF-8 translation

std::string convertStringEncoding(const char* source, uint32_t srcFormat, uint32_t dstFormat)
{
    if(source == NULL)
    {
        return NULL;
    }
#if (defined(_WIN32)
    // Get the converted length
    int numWLen = MultiByteToWideChar(srcFormat, 0, source, -1, NULL, 0);
    wchar_t *pwBuf = new wchar_t[numWLen + 1];
    wmemset(pwBuf, 0, (numWLen + 1));
    // Converte GBK to wchar format
    int result1 = MultiByteToWideChar(srcFormat, 0, source, (size_t)strlen(source), (LPWSTR)pwBuf, numWLen);

    // Get the converted length
    int numcLen = WideCharToMultiByte(dstFormat, 0, pwBuf, -1, NULL, 0, NULL, FALSE);
    char* pcBuf = new char[numcLen + 1];
    memset(pcBuf, 0, numcLen+1);
    // Converte wchar to GBK format
    int result2 = WideCharToMultiByte(dstFormat, 0, pwBuf, numWLen, (LPSTR)pcBuf, numcLen, NULL, FALSE);

    std::string destination(pcBuf);
    delete[] pwBuf;
    delete[] pcBuf;

    if(result1 == 0 || result2 == 0)
        return std::string(source);

    return destination;

#else  // linux

    std::string destination("");
    size_t srcLen = strlen(source);
    if(srcLen == 0)
        return destination;

    iconv_t cd;
    /* Target encoding: For characters that cannot be converted
     *        TRANSLIT:find similar characters to replace
     *        IGNORE  :ignor it*/
    if(srcFormat == ENCODING_GBK)
    {
        // from GBK to UTF-8
        cd = iconv_open("UTF-8//IGNORE", "GBK");
    }
    else
    {
        // from UTF-8 to GBK
        cd = iconv_open("GBK//IGNORE", "UTF-8");
    }

    if(cd == (iconv_t)-1)
        return destination;

    char *pcSrc = const_cast<char*>(source);
    size_t dstLen = 2*srcLen + 2;
    char *pcDst = new char[dstLen];
    memset(pcDst, 0, dstLen); 
    char *pcTemp = pcDst;

    iconv(cd, &pcSrc, &srcLen, &pcTemp, &dstLen);
    iconv_close(cd);

    destination = pcDst;
    delete []pcDst;

    return destination;

#endif
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值