BigInt
BigInt copied to clipboard
BigInt problem with hexdigit string
trafficstars
BigInt header only does not work with hexdigit string like std::string hexStr = "123456789abcdef123456789abcdef"; It is very kind of you to extend it.
Best thanks
int get_val(char c) {
assert(('0' <= c and c <= '9') or ('a' <= c and c <= 'f'));
if ('0' <= c and c <= '9')
return c - '0';
else
return c - 'a' + 10;
}
BigInt hex_to_BigInt(const std::string& hex) {
assert(!hex.empty());
BigInt res = 0, p = 1;
for (auto it = hex.rbegin(); it != hex.rend(); it++) {
res += p * get_val(*it);
p *= 16;
}
return res;
}
I had something similar to this in mind. Do we have any library specific optimizations? Obviously we would need to handle lots of edge cases in original implementation.