您的位置:首页 > 其它

将四个BYTE数值转换成IEEE754标准的浮点数(两种方法:用Addr函数取字节数字的首地址,或者用Absolute关键字)

2015-12-09 23:34 267 查看
在工作中,经常使用到IEEE754格式的数据。IEEE754格式的数据占四个字节,好像Motorola格式和Intel格式的还不一样。

由于工作中很少和他打交道(使用的软件内部已经处理),就没太在意。

今天在编程时发现需要把四个BYTE类型的数据转换成IEEE754标准的数据,就编了一个函数处理一下。

1unitUnit2;
2
3interface
4
5uses
6Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
7Dialogs,StdCtrls;
8
9type
10TForm2=class(TForm)
11edt1:TEdit;
12edt2:TEdit;
13edt3:TEdit;
14edt4:TEdit;
15edt5:TEdit;
16btn1:TButton;
17procedurebtn1Click(Sender:TObject);
18private
19{Privatedeclarations}
20public
21{Publicdeclarations}
22end;
23
24var
25Form2:TForm2;
26
27implementation
28
29{$R*.dfm}
30{将四个BYTE转换成IEEE754格式的数据}
31functionPackByteToFloat(byte1,byte2,byte3,byte4:byte):Single;
32var
33input:array[1..4]ofbyte;{定义一个数组存放输入的四个BYTE}
34output:PSingle;
35begin
36input[1]:=byte1;
37input[2]:=byte2;
38input[3]:=byte3;
39input[4]:=byte4;
40output:=Addr(input);{使用取地址的方法进行处理}
41Result:=output^;{得到了intel格式的数据}
42end;
43
44procedureTForm2.btn1Click(Sender:TObject);
45var
46byte1,byte2,byte3,byte4:byte;
47
48begin
49byte1:=StrToInt(edt1.Text);
50byte2:=StrToInt(edt2.Text);
51byte3:=StrToInt(edt3.Text);
52byte4:=StrToInt(edt4.Text);
53
54edt5.Text:=FloatToStr(PackByteToFloat(byte1,byte2,byte3,byte4));
55
56end;
57
58end.
59

从万一老师的博客上又学到了一招:



http://www.cnblogs.com/dabiao/archive/2010/02/20/1669842.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: