I am writing a program that converts a hexadecimal number to a decimal number (without using the std::hex and std::dec stuff), and at one point need to change from a double to an int.. However when large numbers, such as 5,726,623,060.00000 are entered, they all get changed to -2,147,483,648 after being run through static_cast<int>... Any help as to how to get them to be converted correctly to (i.e 5,726,623,060.00000 to 5,726,623,060) would be very much appreciated!! Thanks!! :)

That number exceeds the largest value of the signed (and even unsigned) 32-bit integer. If you need numbers that large, your instructor may be seeking a solution where you represent your numbers by an array or ints or chars.

Oh, does it also exceed the largest value of a long integer? If so, is there another method in which I can do integer division without the number actually being an integer?? Right now I have the following in a loop with some other code to convert the hex number to a decimal number, and it works for any hex number with less than 8 digits, but if I get more than that it fails. I suppose that must be due to it exceeding the max value of an int, so is there a way for me to do the division in the following code as integer division without the number 'divider' actually being an integer?? Thanks! Code:

divider = static_cast<int>(decimalNum);
tempHexNum = divider % 16;
divider /= 16;

Thanks! :)

Yes, use an int array as an accumulator:

0xFFFF = 15+240+ ...
       = [_|_|_|...|_|1|5]+[_|_|_|...|2|4|0] + ... = [_|_|_|...|2|5|5] + ...

Summing each new calculation into your array (you don't need more than one, just make sure you have things lined up and do the carries properly. This way you increase your capacity dramatically.

Holy... You just gave me an idea as to how to do this in a much much simpler way than I was originally doing it... Wow... Thanks!!! :)

Edited 5 Years Ago by tofugamer: n/a

This article has been dead for over six months. Start a new discussion instead.