BigInt icon indicating copy to clipboard operation
BigInt copied to clipboard

BigInt problem with hexdigit string

Open Thanh-Binh opened this issue 2 years ago • 1 comments
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

Thanh-Binh avatar Jan 10 '23 20:01 Thanh-Binh

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.

whym1here avatar Feb 14 '24 16:02 whym1here