我有两个大的十六进制数字,每个数字至少有 70 位(十进制),我正在尝试计算值 hex_a%hex_b:
string hex_a="1133A5DCDEF3216A63EB879A82F5A1DC4490CCF6412492CF1B242DB";
string hex_b="AAB3A5DCDEF3216A6AAA2F5A1DC4490CCF6412492CF1B242DB";
我尝试使用 stoi 将两个十六进制数字转换为十进制并将值存储在字符串中:
string a=to_string(stoi(hex_a, 0, 16));
但是我在使用 stoi 方法时遇到错误:
terminate called after throwing an instance of 'std::out_of_range'
what(): stoi
Aborted (core dumped)
如何在 C++ 中计算两个大数的模?
首先,这么大的数你不能转换为“int”、“long”、“long long”、“double”等
我看到的唯一方法是在字符串上实现基本的数学运算(+、-、乘16或移位),而不是自己实现除法算法。
参考: https://en.wikipedia.org/wiki/Division_algorithm
或者尝试使用“任意精度算术库”: https://en.wikipedia.org/wiki/List_of_ Arithmetic_software