可以使用 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++ 十六进制字符串转十进制数字再转无符号字符

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

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