Win32字符串编码格式转化

本文详细介绍了ASCII、UTF-8及Unicode编码之间的相互转换方法,并强调了在进行编码转换时需通过Unicode作为中间桥梁的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.ascii转unicode

wstring asciiToUnicode(string asciiStr)
{
    int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, NULL, 0);
    if(0 == widesize)
    {
        return std::wstring();
    }


    std::vector<wchar_t> resultstring(widesize);
    int count = MultiByteToWideChar(CP_ACP, 0, (char*)asciiStr.c_str(), -1, &resultstring[0], widesize);
    if(0 == count)
    {
        return std::wstring();
    }

    return std::wstring(&resultstring[0]);
}

2.utf8转unicode

std::wstring utf8ToUnicode(const std::string &s)
{
    if (s.length() == 0) {
        return wstring();
      }

      // compute the length of the buffer we'll need
      int charcount = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);

      if (charcount == 0) {
        return wstring();
      }

      // convert
      wchar_t* buf = new wchar_t[charcount];
      MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buf, charcount);
      wstring result(buf);
      delete[] buf;
      return result;
}

3.unicode 转ascii

string  unicodeToAscii(wstring unicodeStr)
{
    // compute the length of the buffer we'll need
    int charcount = WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1,
                                      NULL, 0, NULL, NULL);
    if (charcount == 0) {
        return string();
    }

    // convert
    char *buf = new char[charcount];
    WideCharToMultiByte(CP_ACP, 0, unicodeStr.c_str(), -1, buf, charcount,
                      NULL, NULL);

    string result(buf);
    delete[] buf;
    return result;
}

4.unicode转utf8

string  unicodeToUtf8(wstring unicodeStr)
{
    int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, NULL, 0, NULL, NULL);
    if(0 == utf8size)
    {
        return std::string();
    }
    std::vector<char> resultstr(utf8size);
    int count = ::WideCharToMultiByte(CP_UTF8, 0, unicodeStr.c_str(), -1, &resultstr[0], utf8size, NULL, NULL);
    if(0 == count)
    {
        return std::string();
    }

    return std::string(&resultstr[0]);
}

5.ascii转utf8

string PublicFunction::asicToUtf8(string asic)
{
    return unicodeToUtf8(asciiToUnicode(asic));
}

6.utf8转ascii

string utf8ToAsic(string utf8)
{
    return unicodeToAscii(utf8ToUnicode(utf8));
}

总结:转编码格式转化的时候,如果转化的双方不是unicode编码,那么一定要通过unicode做中转,也就是说一定要先转化成unicode,然后再转化成目标编码。
      转化时怎样决定编码(使用CP_UTF8还是CP_ACP),只要是转unicode,那么如果是utf8转unicode(或者unicode转utf8),那么肯定用CP_UTF8;
      如果是ascii转unicode(或者unicode转ascii),那么肯定用CP_ACP。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值