单精度浮点是4字节的,双精度浮点才如楼上所说的前后有4个00,楼上有一点说的是对的,要注意大端 小端问题,最简单的转换方法是使用指针,示例代码如下。var data: Cardinal; pf: PSingle; begin data := $43B6D99A; pf := @data; ShowMessage(FloatToStr(pf^)); end;
Delphi支持absolute addresses,可以直接: var data: Cardinal; f: Single absolute data; begin data := $43B6D99A; ShowMessage(Format('%8.2f', [f])); end;
function HextoFloat(s:string):real; var b,temp:string; e:integer; f:real; begin b:=HextoBinary(s); temp := copy(b,2,8); e:=BintoInt(temp)-127; temp := copy(b,10,23); f := 1+floatBintoInt(temp); if(copy(b,1,1)='0')then result := power(2,e)*f else result :=-power(2,e)*f; end;
还可以用复合结构,比较直观, 单片机一般用单精度浮点,注意高低字节,整形数在电脑里是低字节在前的 //单精度浮点数表示 TSingleRecord = record case Integer of 0: (bLL,bLH,bHL,bHH:Byte); //低字节在前 1: (wl,wh:Word); //低字在前 2: (Value:Single); //浮点数 end;
data: Cardinal;
pf: PSingle;
begin
data := $43B6D99A;
pf := @data;
ShowMessage(FloatToStr(pf^));
end;
var
data: Cardinal;
f: Single absolute data;
begin
data := $43B6D99A;
ShowMessage(Format('%8.2f', [f]));
end;
var b,temp:string;
e:integer;
f:real;
begin
b:=HextoBinary(s);
temp := copy(b,2,8);
e:=BintoInt(temp)-127;
temp := copy(b,10,23);
f := 1+floatBintoInt(temp);
if(copy(b,1,1)='0')then
result := power(2,e)*f
else
result :=-power(2,e)*f;
end;
单片机一般用单精度浮点,注意高低字节,整形数在电脑里是低字节在前的
//单精度浮点数表示
TSingleRecord = record
case Integer of
0: (bLL,bLH,bHL,bHH:Byte); //低字节在前
1: (wl,wh:Word); //低字在前
2: (Value:Single); //浮点数
end;