### how to convert message digest to bigInteger in c++

For example say I have a message digest "cf91cc61b74a1feda9902ae9b9e96d9a" using md5()

How do I convert this message digest to a bigInteger so that I can use reduction function on it.

 12345678910111213141516171819202122232425262728293031 #include #include #include int int_value( char hex_digit ) // invariant: valid hex digit { if( hex_digit >= '0' && hex_digit <= '9' ) return hex_digit - '0' ; static const std::string af = "abcdefABCDEF" ; const auto p = af.find(hex_digit) ; return p%6 + 10 ; } boost::multiprecision::cpp_int to_big_int( const std::string& hex_str ) // invariant: valid hex string { boost::multiprecision::cpp_int big_int = 0 ; for( char hex_digit : hex_str ) { big_int *= 16 ; big_int += int_value(hex_digit) ; } return big_int ; } int main() { const std::string hex_str = "cf91cc61b74a1feda9902ae9b9e96d9a" ; std::cout << to_big_int(hex_str) << '\n' ; }
Boost Multiprecision understands hexadecimal strings without need for help:

 123456789101112131415 #include #include #include using integer = boost::multiprecision::cpp_int; int main() { const std::string s = "cf91cc61b74a1feda9902ae9b9e96d9a"; integer n = integer( "0x" + s ); std::cout << "s (hex) = " << s << "\n"; std::cout << "n (dec) = " << std::dec << n << "\n"; std::cout << "n (hex) = " << std::hex << n << "\n"; }

@JLBorges
I’m not convinced that testing the Arabic digits separately has much advantage compared to just putting them in the lookup string... but even so, I honestly don’t know of any hardware where A..F and a..f are not consecutively sequential.
Last edited on
Do i have to install the c++ boost library on my desktop to use it?

EDIT:
is alright. i copy and paste the file in the mingw directory.
Last edited on
Boost's multiprecision library does require being compiled to be fully functional.

There are several BigInt libraries that are header only.