home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TBTREE.ZIP / HEX.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-07-25  |  2.5 KB  |  91 lines

  1. (* TBTree13             Copyright (c)  1988            Dean H. Farwell II    *)
  2.  
  3. unit Hex;
  4.  
  5. (*****************************************************************************)
  6. (*                                                                           *)
  7. (*                H E X  C O N V E R S I O N  R O U T I N E S                *)
  8. (*                                                                           *)
  9. (*****************************************************************************)
  10.  
  11.  
  12. (* These routine support conversion from/to hex from byte values             *)
  13.  
  14.  
  15. (* Version Information
  16.  
  17.    Version 1.1 - No Changes
  18.  
  19.    Version 1.2 - No Changes
  20.  
  21.    Version 1.3 - No Changes                                                  *)
  22.  
  23.  
  24. (*////////////////////////// I N T E R F A C E //////////////////////////////*)
  25.  
  26. interface
  27.  
  28. type
  29.     HexArray = String[2];
  30.  
  31.  
  32. (* Finds the hex character for a given 4 bit value
  33.    for example - GetHexChar(11) = 'B'                                       *)
  34.  
  35. function GetHexChar(var x : Byte) : Char;
  36.  
  37.  
  38. (* This function returns the hex value for an associated 8 bit byte.  The value
  39.    is returned as a string of type HexArray (2 bytes)
  40.    for example - ByteToHex(255) = 'FF'                                      *)
  41.  
  42. function ByteToHex(x : Byte) : HexArray;
  43.  
  44. (*\*)
  45. (*///////////////////// I M P L E M E N T A T I O N /////////////////////////*)
  46.  
  47. implementation
  48.  
  49.  
  50. (* Finds the hex character for a given 4 bit value
  51.    for example - GetHexChar(11) = 'B'                                       *)
  52.  
  53. function GetHexChar(var x : Byte) : Char;
  54.  
  55. var
  56.     result1 : Byte;
  57.     result2 : Char absolute result1;
  58.  
  59.     begin
  60.     if (x >= 0) and (x <=9) then
  61.         begin
  62.         result1 := x + 48;
  63.         end
  64.     else
  65.         begin
  66.         result1 := x + 55;
  67.         end;
  68.     GetHexChar := result2;
  69.     end;                                        (* End of GetHexChar Routine *)
  70.  
  71.  
  72. (* This function returns the hex value for an associated 8 bit byte.  The value
  73.    is returned as a string of type HexArray (2 bytes)
  74.    for example - ByteToHex(255) = 'FF'                                      *)
  75.  
  76. Function ByteToHex(x : Byte) : HexArray;
  77.  
  78. var
  79.     low,
  80.     high : Byte;
  81.  
  82.     begin
  83.     high := x div 16;
  84.     low  := x mod 16;
  85.     ByteToHex := GetHexChar(high) + GetHexChar(low);
  86.     end;                                         (* End of ByteToHex Routine *)
  87.  
  88.  
  89. end.                                                      (* end of Hex unit *)
  90.  
  91.