问题
IF I have a IEEE float hex 42F6E979, how do I convert it to decimal? I believe the decimal representation is = 123.456001
回答1:
(Most) assembly language doesn't really enforce types very strongly, so you can just initialize a location with that value, and then treat/use it as 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 produces binary coded decimal, so you have to split each byte into two digits for display. Of course, this is for x86 -- most other architectures make the job a tad more difficult. For example, on PowerPC, you have fctiw
, which just converts to an integer (on the same general order as the x86 fist
). To use it, you'd normally multiply by some multiple of 10 to get the number of decimal places you want, then convert to integer, and put a decimal point in the right place in the result. It can get a bit ugly when/if 1) you're close to the limits of the range for a floating point number, or 2) you want more precision than you can represent in a single integer.
回答2:
While you're testing out your own code, you can use this online tool to check your answer. FYI, you are correct in that the decimal is 123.45...
Wikipedia has a useful article as well.
回答3:
See Wikipedia or call the library routine that probably already exists in your environment. It's a wheel often re-invented
回答4:
I'll do this in C, because you haven't provided a 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.
来源:https://stackoverflow.com/questions/3892132/convert-ieee-float-hex-to-decimal