I have to write a function to convert a float number to hexadecimal. The problem is I don't know how to convert a float number to hexadecimal although I can convert integers. Can anyone help?
Printable View
I have to write a function to convert a float number to hexadecimal. The problem is I don't know how to convert a float number to hexadecimal although I can convert integers. Can anyone help?
To convert from decimal to hex (as far as I remember) you keep dividing the quotent by 16 and save the remainder until you get to Zero. You then read the result from the bottom up. Now you divide the remainder(after the decimal point) by 16 and save the remainder until you get to Zero. This you read from the top down.
EG
159.62decimal =
159/16 = 9 r 15 = F
9/16 = 0 r 9 = 9
read up therefor 159 = 9F in hex
now
62/16 = 3 r 14 = E
3/16=0 r 9
read down therefor 62 = E 9
Therefore 159.62 decimal = 9F.E9 hex
THis is how you convert decimal to binary and i think that this applies to any base.
Colin
Or you could just do the following:
cout<<hex<<number;
:D