home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PASCAL / TBTREE16.ZIP / HEX.PAS < prev    next >
Pascal/Delphi Source File  |  1989-07-13  |  3KB  |  97 lines

  1. (* TBTree16             Copyright (c)  1988,1989       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 routines 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.    Version 1.4 - No Changes
  24.  
  25.    Version 1.5 - No Changes
  26.  
  27.    Version 1.6 - No Changes                                                  *)
  28.  
  29. (*////////////////////////// I N T E R F A C E //////////////////////////////*)
  30.  
  31. interface
  32.  
  33. type
  34.     HexArray = String[2];
  35.  
  36.  
  37. (* Finds the hex character for a given 4 bit value
  38.    for example - GetHexChar(11) = 'B'                                       *)
  39.  
  40. function GetHexChar(var x : Byte) : Char;
  41.  
  42.  
  43. (* This function returns the hex value for an associated 8 bit byte.  The value
  44.    is returned as a string of type HexArray (2 bytes)
  45.    for example - ByteToHex(255) = 'FF'                                      *)
  46.  
  47. function ByteToHex(x : Byte) : HexArray;
  48.  
  49. (*!*)
  50. (*\*)
  51. (*///////////////////// I M P L E M E N T A T I O N /////////////////////////*)
  52.  
  53. implementation
  54.  
  55.  
  56. (* Finds the hex character for a given 4 bit value
  57.    for example - GetHexChar(11) = 'B'                                       *)
  58.  
  59. function GetHexChar(var x : Byte) : Char;
  60.  
  61. var
  62.     result1 : Byte;
  63.     result2 : Char absolute result1;
  64.  
  65.     begin
  66.     if (x >= 0) and (x <=9) then
  67.         begin
  68.         result1 := x + 48;
  69.         end
  70.     else
  71.         begin
  72.         result1 := x + 55;
  73.         end;
  74.     GetHexChar := result2;
  75.     end;                                        (* End of GetHexChar Routine *)
  76.  
  77.  
  78. (* This function returns the hex value for an associated 8 bit byte.  The value
  79.    is returned as a string of type HexArray (2 bytes)
  80.    for example - ByteToHex(255) = 'FF'                                      *)
  81.  
  82. Function ByteToHex(x : Byte) : HexArray;
  83.  
  84. var
  85.     low,
  86.     high : Byte;
  87.  
  88.     begin
  89.     high := x div 16;
  90.     low  := x mod 16;
  91.     ByteToHex := GetHexChar(high) + GetHexChar(low);
  92.     end;                                         (* End of ByteToHex Routine *)
  93.  
  94.  
  95. end.                                                      (* end of Hex unit *)
  96.  
  97.