c++相關(guān)編碼轉(zhuǎn)換

十六進(jìn)制轉(zhuǎn)ascii

std::string hexToAscii(const std::string& hexString) {
    std::string asciiString;

    for (size_t i = 0; i < hexString.length(); i += 2) {
        if(hexString[i]=='\\')
        {
            continue;
        }
        // 從兩個(gè)字符表示的十六進(jìn)制數(shù)轉(zhuǎn)換為整數(shù)
        int decimal = std::stoi(hexString.substr(i, 2), nullptr, 16);

        // 將整數(shù)轉(zhuǎn)換為對(duì)應(yīng)的 ASCII 字符
        asciiString += static_cast<char>(decimal);
    }

    return asciiString;
}

unicode轉(zhuǎn)UTF8

std::string unicodeToUTF8(const std::string& unicodeStr)
{
    std::string utf8Str;
    for (std::size_t i = 0; i < unicodeStr.size(); i += 6)
    {
        std::string unicodeCode = unicodeStr.substr(i + 2, 4);
        unsigned long unicodeValue = std::stoul(unicodeCode, nullptr, 16);
        if (unicodeValue <= 0x007F)
        {
            utf8Str += static_cast<char>(unicodeValue);
        }
        else if (unicodeValue <= 0x07FF) {
            utf8Str += static_cast<char>(0xC0 | ((unicodeValue >> 6) & 0x1F));
            utf8Str += static_cast<char>(0x80 | (unicodeValue & 0x3F));
        }
        else {
            utf8Str += static_cast<char>(0xE0 | ((unicodeValue >> 12) & 0x0F));
            utf8Str += static_cast<char>(0x80 | ((unicodeValue >> 6) & 0x3F));
            utf8Str += static_cast<char>(0x80 | (unicodeValue & 0x3F));
        }
    }
    return utf8Str;
}

UTF8轉(zhuǎn)unicode

std::string to_hex_string(unsigned long value, int width)
{
    std::stringstream ss;
    ss << std::hex << std::uppercase << std::setw(width) << std::setfill('0') << value;

    std::string hexStr = "\\u" + ss.str();

    // 轉(zhuǎn)換為小寫形式
    for (char& c : hexStr)
    {
        c = std::tolower(c);
    }

    return hexStr;
}

std::string utf8ToUnicode(const std::string& utf8Str)
{
    std::string unicodeStr;
    std::size_t i = 0;
    while (i < utf8Str.size())
    {
        unsigned char byte1 = static_cast<unsigned char>(utf8Str[i]);
        if ((byte1 & 0x80) == 0) // 1個(gè)字節(jié)
        {
            unicodeStr += utf8Str[i];
            i++;
        }
        else if ((byte1 & 0xE0) == 0xC0) // 2個(gè)字節(jié)
        {
            unsigned char byte2 = static_cast<unsigned char>(utf8Str[i + 1]);
            unsigned long unicodeValue = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
            unicodeStr += to_hex_string(unicodeValue, 4); // 轉(zhuǎn)換為Unicode編碼
            i += 2;
        }
        else if ((byte1 & 0xF0) == 0xE0) // 3個(gè)字節(jié)
        {
            unsigned char byte2 = static_cast<unsigned char>(utf8Str[i + 1]);
            unsigned char byte3 = static_cast<unsigned char>(utf8Str[i + 2]);
            unsigned long unicodeValue = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
            unicodeStr += to_hex_string(unicodeValue, 4); // 轉(zhuǎn)換為Unicode編碼
            i += 3;
        }
        else
        {
            // 不支持的UTF-8編碼格式
            // 可以根據(jù)需要進(jìn)行處理或拋出異常
            break;
        }
    }
    return unicodeStr;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容