美文网首页
c++相关编码转换

c++相关编码转换

作者: c之气三段 | 来源:发表于2023-09-03 16:21 被阅读0次

    十六进制转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;
            }
            // 从两个字符表示的十六进制数转换为整数
            int decimal = std::stoi(hexString.substr(i, 2), nullptr, 16);
    
            // 将整数转换为对应的 ASCII 字符
            asciiString += static_cast<char>(decimal);
        }
    
        return asciiString;
    }
    

    unicode转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转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();
    
        // 转换为小写形式
        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个字节
            {
                unicodeStr += utf8Str[i];
                i++;
            }
            else if ((byte1 & 0xE0) == 0xC0) // 2个字节
            {
                unsigned char byte2 = static_cast<unsigned char>(utf8Str[i + 1]);
                unsigned long unicodeValue = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
                unicodeStr += to_hex_string(unicodeValue, 4); // 转换为Unicode编码
                i += 2;
            }
            else if ((byte1 & 0xF0) == 0xE0) // 3个字节
            {
                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); // 转换为Unicode编码
                i += 3;
            }
            else
            {
                // 不支持的UTF-8编码格式
                // 可以根据需要进行处理或抛出异常
                break;
            }
        }
        return unicodeStr;
    }
    

    相关文章

      网友评论

          本文标题:c++相关编码转换

          本文链接:https://www.haomeiwen.com/subject/eijbmdtx.html