可以使用 stoi 函数将十六进制字符串转换为十进制数字,然后使用 static_cast 将其转换为无符号 char。

示例代码:

#include <iostream>
#include <string>

int main() {
    std::string hexStr = "a1";
    int decNum = std::stoi(hexStr, nullptr, 16);
    std::cout << "Decimal number: " << decNum << std::endl;
    unsigned char ch = static_cast<unsigned char>(decNum);
    std::cout << "Unsigned char: " << ch << std::endl;
    return 0;
}

输出结果:

Decimal number: 161
Unsigned char: ¡
c++ 如何将 十六进制字符串转换为十进制数字再转换成无符号char

原文地址: https://www.cveoy.top/t/topic/eb1p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录