gpt4 book ai didi

c++ - 将基数 11 转换为十进制

转载 作者:行者123 更新时间:2023-11-28 02:10:31 25 4
gpt4 key购买 nike

我知道 unsigned long 最多只有 4294967295,所以这是我的问题,当用户输入很多数字(比如结果将超过限制)时,转换后的数字将是只是 4294967295。示例:

Base 11: 1928374192847
Decimal: 4294967295

结果应该是 5777758712535。如何解决这个限制?我们需要vb6.0。这是我的代码:

    cout << "\t\t  CONVERSION\n";
cout << "\t Base 11 to Decimal\n";
cout << "\nBase 11: ";
cin >> str;
const auto x = str.find_first_not_of("0123456789aA");
if (x != string::npos)
{
std::cout << "Invalid Input\n\n";
goto a;
}
unsigned long x = strtoul(str.c_str(), NULL, 11);
cout << "Decimal: " << x << "\n\n";

如果结果超过 4294967295,程序应该说“超出范围”。对不起,我只是一个初学者。

最佳答案

来自strtoul docs on www.cplusplus.com :

If the value read is out of the range of representable values by an unsigned long int, the function returns ULONG_MAX (defined in <climits>), and errno is set to ERANGE.

您应该检查 errno 以确定该值是否超出范围。

关于c++ - 将基数 11 转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35897338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com