home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / DECODEBA.ZIP / DECODE.PAS next >
Encoding:
Pascal/Delphi Source File  |  1988-01-20  |  3.3 KB  |  96 lines

  1. --------------------------------------------------------------------------
  2.                        F i l e    I n f o r m a t i o n
  3.  
  4. * DESCRIPTION
  5. Used in Turbo programs which read files created by BASIC programs, and
  6. rewrites them in Turbo Pascal format.
  7. Requires: Turbo Pascal 2.0, 3.0. Author: Rick Solomon. Version T1.0
  8.  
  9. * ASSOCIATED FILES
  10. DECODE.PAS
  11. DECODE88.PAS
  12. DECODE.TXT
  13.  
  14. ==========================================================================
  15. }
  16. {.PL65}
  17. {The following function (DECODE) converts REAL numbers stored
  18.  by a BASIC program in a random access file (i.e. in ASCII
  19.  format) and read by a Pascal program into a number which
  20.  can be used by Pascal. BASIC stores a REAL (single precision)
  21.  number as a four byte vector starting with the least
  22.  significant byte in the first position to the most significant
  23.  byte in third position with the exponent in fourth. TURBO
  24.  Pascal uses a six byte format starting with the exponent in
  25.  position 0 then least to most significant bytes in positions
  26.  one through five respectively.
  27.  
  28.  Since BASIC and TURBO use the same encoding formula, an
  29.  alternative method would be to place the bytes read from
  30.  the BASIC file directly into the appropriate locations
  31.  in a REAL variable using absolute addressing. This option
  32.  is not available to TURBO-87/TURBOBCD users, however, since
  33.  TURBO-87/TURBOBCD use an eight/ten byte REALs with entirely
  34.  different encoding.
  35.  
  36.  DECODE uses the following formula:
  37.  
  38.    ((MSB*2^16 + MID*2^8 + LSB)/2^24) * 2^(EXP-128)
  39.  
  40.     MSB : Most significant byte;
  41.     LSB : Least significant byte;
  42.     MID : Middle byte;
  43.     EXP : Exponent.
  44.  
  45.  A global Type Short_Vector = Array[1..4] of Byte must be
  46.  declared in the calling routine and used To pass the values
  47.  to the Function.}
  48.  
  49. Function Decode(Basic_Real:Short_VecTor):Real;
  50. Var
  51.   Zero : Boolean;
  52.  
  53.   Function Work_It_Out(Basic_No:Short_VecTor):Real;
  54.    {Computes all non Zero Reals}
  55.   Const
  56.     Two8  = 256.0;      {2^8}
  57.     Two16 = 65536.0;    {2^16}
  58.     Two24 = 16777216.0; {2^24}
  59.   Type
  60.     Sign_Type = (Positive,Negative);
  61.   Var
  62.     Temp               : Real;
  63.     Sign               : Sign_Type;
  64.     I                  : Integer;
  65.     Msb, Lsb, Mid, Exp : Byte;
  66.   Begin
  67.     Sign:= Positive;
  68.     Exp:=  Basic_No[4];
  69.     Msb:=  Basic_No[3];
  70.     Mid:=  Basic_No[2];
  71.     Lsb:=  Basic_No[1];
  72.     { The Sign of the number is sTored in bit 7 of Msb. 0 is
  73.       Positive, 1 is Negative. This bit must be checked prior
  74.       To calculation, and switched To 1 if it is Zero. }
  75.     IF Msb < 128   {i.e. if bit 7 = 0}
  76.       Then Msb := Msb + 128  {put a 1 in bit 7}
  77.       Else Sign := Negative;
  78.     Temp := ((Msb*Two16) + (Mid*Two8) + Lsb)/(Two24);
  79.     IF Exp > 128 Then For I:= 129 To Exp do Temp:= Temp * 2;
  80.     IF Exp < 128 Then For I:= Exp To 127 do Temp:= Temp/2;
  81.     Case Sign Of
  82.       Positive : Work_It_Out := Temp;
  83.       Negative : Work_It_Out := Temp * -1;
  84.     End; {Case}
  85.   End; {Function Work_It_Out}
  86.  
  87. Begin {Function Decode}
  88.   {Check For Zero Exponent. If the Exponent is 0 the num is 0 by definition}
  89.   Zero := (Basic_Real[4] = 0);
  90.   Case Zero Of
  91.     True  : Decode := 0;
  92.     False : Decode := Work_It_Out(Basic_Real);
  93.   End; {Case}
  94. End; {Function Decode}
  95. 
  96.