Convert IEEE float hex to decimal?

拟墨画扇 提交于 2019-12-01 11:31:25

(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.

Pat

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!