home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / NUTUG11.ZIP / BASECON.PAS next >
Encoding:
Pascal/Delphi Source File  |  1987-11-22  |  4.5 KB  |  151 lines

  1. Unit BaseCon;
  2.  
  3.             {NORTHWESTERN UNIVERSITY TURBO USERS GROUP UTILITIES}
  4.  
  5.                       (** NUtility BASECON ROUTINES **)
  6.  
  7.                            {(C) J. E Hilliard 1986}
  8.  
  9.  
  10.          {This is a set of functions for performing the following con-
  11.          versions: Dec > Hex; Dec > Bin and BCD > Dec.               }
  12.  
  13. Interface
  14.  
  15. FUNCTION OutHex (Dec : integer) : String;
  16.          {Returns the hexadecimal value of a decimal integer or byte
  17.          as a string with the prefix '$'.                           }
  18.  
  19. FUNCTION OutBin (Dec : integer) : String;
  20.          {Returns the binary value of 'Dec' as a string.             }
  21.  
  22. FUNCTION InBCD (Input : integer) : integer;
  23.  
  24.          {This function returns the value of a Binary Coded Decimal
  25.          integer or byte. The BCD format is frequently used by Clock/
  26.          Calendar boards.                                            }
  27.  
  28.          {/Numbers are normally stored in binary format. An alterna-
  29.          tive format is Binary Coded Decimal (BCD). Four bits are
  30.          assigned to each decimal digit (nibbles $0 to $9). The six
  31.          nibbles $A to $F are not used. This is obviously an unecon-
  32.          omical use of storage. But BCD's have advantages, which
  33.          include easy conversion to ASCII code and a well defined
  34.          precision.                                                  /}
  35.  
  36. Implementation
  37.  
  38. TYPE
  39.  
  40.   String20 = string[20];
  41.   String10 = string[10];
  42.  
  43. FUNCTION OutHex (Dec : integer) : String;
  44.  
  45.          {Returns the hexadecimal value of a decimal integer or byte
  46.          as a string with the prefix '$'.                           }
  47.  
  48. VAR
  49.  
  50.   Int1    : integer;
  51.   HoldS   : String10;
  52.  
  53. Begin
  54.  
  55.   HoldS := '';
  56.  
  57.   repeat
  58.  
  59.     Int1 := Dec and $F;                          {Mask off low byte.        }
  60.     if Int1 < 10
  61.       then                                       {Numeric reqd.             }
  62.         HoldS := chr (48 + Int1) + HoldS         {#48 = '0'.                }
  63.       else                                       {Character reqd.           }
  64.         HoldS := chr (55 + Int1) + HoldS;        {#65 = 'A'.                }
  65.     Dec := Dec shr 4;                            {Get next byte if any.     }
  66.  
  67.   until Dec = 0;
  68.  
  69.   if length (HoldS) mod 2 <> 0
  70.     then                                         {Append leading 0 if nec.  }
  71.       HoldS := '0' + HoldS;
  72.  
  73.   OutHex := '$' + HoldS;                         {Append 'H' if preferred.  }
  74.  
  75. End; {OutHex (Dec : integer) : String10}
  76.  
  77.  
  78. FUNCTION OutBin (Dec : integer) : String;
  79.  
  80.          {Returns the binary value of 'Dec' as a string.             }
  81.  
  82. VAR
  83.  
  84.   J     : integer;
  85.   Int1  : integer;
  86.   HoldS : string[20];
  87.  
  88. Begin
  89.  
  90.   HoldS := '';
  91.   Int1  := Dec;
  92.   repeat
  93.  
  94.     for J := 1 to 8 do
  95.       begin
  96.         if odd (Int1)                  {Interesting application of 'odd'.   }
  97.           then
  98.             HoldS := '1' + HoldS
  99.           else
  100.             HoldS := '0' + HoldS;
  101.         Int1 := Int1 shr 1;            {Get next bit.                       }
  102.       end; {for J}
  103.     if (Dec > 127) or (Dec < 0)
  104.       then
  105.         begin
  106.           HoldS := ' ' + HoldS;        {Add separator between the two sets  }
  107.           Dec := 0;                    {of 8 bits.                          }
  108.         end; {if}
  109.  
  110.   until Int1 = 0;
  111.  
  112.   OutBin := HoldS;
  113.  
  114. End; {OutBin (Dec : byte) : String20}
  115.  
  116.  
  117. FUNCTION InBCD (Input : integer) : integer;
  118.  
  119.          {This function returns the value of a Binary Coded Decimal
  120.          integer or byte. The BCD format is frequently used by Clock/
  121.          Calendar boards.                                            }
  122.  
  123.          {/Numbers are normally stored in binary format. An alterna-
  124.          tive format is Binary Coded Decimal (BCD). Four bits are
  125.          assigned to each decimal digit (nibbles $0 to $9). The six
  126.          nibbles $A to $F are not used. This is obviously an unecon-
  127.          omical use of storage. But BCD's have advantages, which
  128.          include easy conversion to ASCII code and a well defined
  129.          precision.                                                  /}
  130.  
  131.  
  132. VAR
  133.  
  134.   Int1 : integer;
  135.  
  136. Begin
  137.  
  138.   Int1 := Lo (Input) and $F + 10 * ( Lo (Input) shr 4);
  139.   if Hi (Input) > 0                    {Don't waste time if Input is a byte. }
  140.     then
  141.         Int1 := Int1 + 100 * ( Hi (Input) and $F) +
  142.             1000 * ( Hi (Input) shr 4);
  143.  
  144.   InBCD := Int1;
  145.  
  146. End; {InBCD (Input : integer) : integer}
  147.  
  148. Begin  (* Of Initialization *)
  149. End.
  150.  
  151.