首页  编辑  

Basic浮点数和Delphi浮点数的转换

Tags: /超级猛料/Format.格式,单位/   Date Created:
Convert Float format
Q:
I have a real number stored in 'Microsoft Basic Format'
This contains four bytes. How do I convert this to a single type value in
pascal?
A:
Try with this (I don't full test it):
Unit BasConv;
Interface
 Function BasicReal2Long(InValue: LongInt): LongInt;
 Function Long2BasicReal(InValue: LongInt): LongInt;
Implementation
Function BasicReal2Long(InValue: LongInt): LongInt;
 Var
 Temp: LongInt;
 Expon: Integer;
 Begin
 Expon := ((InValue shr 24) and $ff) - 152;
 Temp := (InValue and $007FFFFF) or $00800000;
 If Expon < 0 Then
   Temp := Temp shr Abs(Expon)
 Else
   Temp := Temp shl Expon;
 If (InValue and $00800000) <> 0 Then
   BasicReal2Long := -Temp
 Else
   BasicReal2Long := Temp;
 If Expon = 0 Then
   BasicReal2Long := 0;
 End;
Function Long2BasicReal(InValue: LongInt): LongInt;
 Var
 Negative: Boolean;
 Expon: LongInt;
 Begin
 If InValue = 0 Then
   Long2BasicReal := 0
 Else
   Begin
   If InValue < 0 Then
     Begin
     Negative := True;
     InValue := Abs(InValue);
     End
   Else
     Negative := False;
   Expon := 152;
   If InValue < $007FFFFF Then
     While ((InValue and $00800000) = 0) Do
       Begin
       InValue := InValue shl 1;
       Dec(Expon);
       End
   Else
     While ((InValue And $FF000000) <> 0) Do
       Begin
       InValue := InValue shr 1;
       Inc(Expon);
       End;
   InValue := InValue And $007FFFFF;
   If Negative Then
     InValue := InValue Or $00800000;
   Long2BasicReal := InValue + (Expon shl 24);
   End;
 End;
End.