可以使用 C++ 标准库中的 std::stoul 函数将十六进制字符串转换为无符号整数。示例代码如下所示:

#include <iostream>
#include <string>

int main() {
    std::string hex_str = "1A";
    unsigned int dec_num = std::stoul(hex_str, nullptr, 16);
    std::cout << dec_num << std::endl;
    return 0;
}

输出结果为:

26

其中,std::stoul 函数接受三个参数,分别为:

  • str:需要转换的字符串;
  • pos:可选参数,指定从字符串的哪个位置开始转换,默认为 0;
  • base:可选参数,指定进制,默认为 10。

在上面的示例代码中,我们将第三个参数设为 16,表示要将十六进制字符串转换为十进制数字。然后将得到的十进制数字赋值给无符号整数变量 dec_num,最后输出结果。

c++ 如何将 十六进制字符串转换为十进制数字再转换成无符号整数

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

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