字节数组转浮点数或整数(200分)

N

nslhw

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个4字节的浮点数,按字节从高到低取出来存在字节数组中,如何将这个数组转换恢复成浮点数
(实际使用是delphi中使用串口访问控件,将数据读到字节数组中,需要恢复数据.)
如var SM:array of byte;
UA:single(real);
setlength(SM,4);
SM[0]:=$42;
SM[1]:=$9F;
SM[2]:=$E9;
SM[3]:=$7B;
UA:=??
 
var
SM:array of Byte;
UA:Single;
P:pByte;
I:Byte;
begin
SetLength(SM,4);
SM[0]:=$42;
SM[1]:=$9F;
SM[2]:=$E9;
SM[3]:=$7B;
P:=@UA;
for I:=0 to 3do
begin
P^:=SM;
Inc(P);
end;
Caption:=FloatToStr(UA);
end;
 
方法二:
var
SM:array of Byte;
UA:Single;
P:pSingle;
begin
SetLength(SM,4);
SM[0]:=$42;
SM[1]:=$9F;
SM[2]:=$E9;
SM[3]:=$7B;
P:=Pointer(SM);
UA:=P^;
Caption:=FloatToStr(UA);
end;
 
接受答案了.
 
就一句话:
Move(PChar(SM)^, UA, SizeOf(Single));
套用上面的例子:
var
SM:array of Byte;
UA:Single;
begin
SetLength(SM,4);
SM[0]:=$42;
SM[1]:=$9F;
SM[2]:=$E9;
SM[3]:=$7B;
Move(PChar(SM)^, UA, SizeOf(Single));
Caption:=FloatToStr(UA);
end;
 

Similar threads

S
回复
0
查看
956
SUNSTONE的Delphi笔记
S
S
回复
0
查看
779
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
710
DelphiTeacher的专栏
D
顶部