Convert IEEE float hex to decimal?

IF I have IEEE float hex 42F6E979, how do I convert it to decimal? I believe decimal representation = 123.456001

+2


source to share


4 answers


While you are testing your own code, you can use this online tool to check your answer. FYI, you are correct that the decimal is 123.45 ...



Wikipedia has a helpful article .

+3


source


(Most) assembly language is not very type-heavy, so you can just initialize the location with that value and then process / use it like a float. The easiest way to convert is usually something like:

.data

myfloat dd 042F6E979H
mydec   db 10 dup(?)

.code

mov ebx, offset mydec    
fld myfloat
fbstp [ebx]

      



This actually results in a binary encoded decimal value, so you need to split each byte into two digits for display. Of course, this is for x86 - most other architectures make the job even harder. For example, on PowerPC you have fctiw

one that just converts to an integer (in the same general order as x86 fist

). To use it, you usually multiply by several multiples of 10 to get the number of decimal places you want, then convert to an integer and put the decimal point where you want it in the result. It can get a little ugly if / if 1) you are close to the range bounds for a floating point number, or 2) you need higher precision than you can imagine in single integers.

+3


source


See Wikipedia or call a library routine that probably already exists in your environment. It's a wheel often reinvented

+1


source


I'll do it in C because you didn't specify the language.

char hex[];
int *ptr;
for(int i = 0; i < 8; ++i)
{
   &ptr += toNumber(hex[i]) << (i * 4);
}

float *value = (float*)ptr;

      

I'll leave it up to the OP to write the toNumber function.

0


source







All Articles