0

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!! :)

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by tofugamer
Featured Replies
  • 2
    jonsca 1,059   6 Years Ago

    Yes, use an int array as an accumulator: [code] 0xFFFF = 15+240+ ... = [_|_|_|...|_|1|5]+[_|_|_|...|2|4|0] + ... = [_|_|_|...|2|5|5] + ... [/code] 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 … Read More

0

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.

0

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! :)

2

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.

0

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 by tofugamer: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.