I am trying to convert 45 bit binary number into a hex number but when compiling, I get overflow error, but when applying the code on online C++ compiler, it works. My platform is X64. Any help please.
int main()
{
stringstream ss;
string binary_str("111000000100010010100000110101001000100011000");
bitset<45> n(binary_str);
string f;
ss << hex << n.to_ulong() << endl; // error happens here
f = ss.str();
cout << f;
return 0;
}
When compile this code above on online C++ compiler I get a correct result which is OX1c08941a9118.
unsigned long
is 32bit with MSVC. Also when compiling for x64. You need unsigned long long
to get a 64bit integer, so in this case you can use to_ullong
:
ss << hex << n.to_ullong() << endl;