在工作中,經常使用到IEEE754格式的數據。IEEE754格式的數據占四個字節,好像Motorola格式和Intel格式的還不一樣。
由於工作中很少和他打交道(使用的軟件內部已經處理),就沒太在意。
今天在編程時發現需要把四個BYTE類型的數據轉換成IEEE754標准的數據,就編了一個函數處理一下。
1 unit Unit2;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9 type
10 TForm2 = class(TForm)
11 edt1: TEdit;
12 edt2: TEdit;
13 edt3: TEdit;
14 edt4: TEdit;
15 edt5: TEdit;
16 btn1: TButton;
17 procedure btn1Click(Sender: TObject);
18 private
19 { Private declarations }
20 public
21 { Public declarations }
22 end;
23
24 var
25 Form2: TForm2;
26
27 implementation
28
29 {$R *.dfm}
30 {將四個BYTE轉換成IEEE754格式的數據}
31 function PackByteToFloat(byte1,byte2,byte3,byte4:byte):Single;
32 var
33 input:array[1..4] of byte; {定義一個數組存放輸入的四個BYTE}
34 output:PSingle;
35 begin
36 input[1] := byte1;
37 input[2] := byte2;
38 input[3] := byte3;
39 input[4] := byte4;
40 output := Addr(input); {使用取地址的方法進行處理}
41 Result := output^; {得到了intel格式的數據}
42 end;
43
44 procedure TForm2.btn1Click(Sender: TObject);
45 var
46 byte1,byte2,byte3,byte4:byte;
47
48 begin
49 byte1 := StrToInt(edt1.Text);
50 byte2 := StrToInt(edt2.Text);
51 byte3 := StrToInt(edt3.Text);
52 byte4 := StrToInt(edt4.Text);
53
54 edt5.Text := FloatToStr(PackByteToFloat(byte1,byte2,byte3,byte4));
55
56 end;
57
58 end.
59
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9 type
10 TForm2 = class(TForm)
11 edt1: TEdit;
12 edt2: TEdit;
13 edt3: TEdit;
14 edt4: TEdit;
15 edt5: TEdit;
16 btn1: TButton;
17 procedure btn1Click(Sender: TObject);
18 private
19 { Private declarations }
20 public
21 { Public declarations }
22 end;
23
24 var
25 Form2: TForm2;
26
27 implementation
28
29 {$R *.dfm}
30 {將四個BYTE轉換成IEEE754格式的數據}
31 function PackByteToFloat(byte1,byte2,byte3,byte4:byte):Single;
32 var
33 input:array[1..4] of byte; {定義一個數組存放輸入的四個BYTE}
34 output:PSingle;
35 begin
36 input[1] := byte1;
37 input[2] := byte2;
38 input[3] := byte3;
39 input[4] := byte4;
40 output := Addr(input); {使用取地址的方法進行處理}
41 Result := output^; {得到了intel格式的數據}
42 end;
43
44 procedure TForm2.btn1Click(Sender: TObject);
45 var
46 byte1,byte2,byte3,byte4:byte;
47
48 begin
49 byte1 := StrToInt(edt1.Text);
50 byte2 := StrToInt(edt2.Text);
51 byte3 := StrToInt(edt3.Text);
52 byte4 := StrToInt(edt4.Text);
53
54 edt5.Text := FloatToStr(PackByteToFloat(byte1,byte2,byte3,byte4));
55
56 end;
57
58 end.
59
http://www.cnblogs.com/dabiao/archive/2010/02/20/1669842.html


procedureTForm1.FormCreate(Sender: TObject);varaa:recordb1,b2,b3,b4:Byteend;bb:Singleabsolute aa;beginaa.b1 :=$CD;aa.b2 :=$CC;aa.b3 :=$44;aa.b4 :=$41;ShowMessagefmt('%f',[bb]);end;