home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TPTOOL3.ZIP / ITOH.INC < prev    next >
Encoding:
Text File  |  1987-03-28  |  1.4 KB  |  77 lines

  1.  
  2. #log Integer to hex conversion
  3.  
  4. (******************************************************
  5.  *
  6.  * Procedure:  itoh
  7.  *
  8.  * Purpose:    converts an integer into a string of hex digits
  9.  *
  10.  * Example:    s := itoh(i);
  11.  *
  12.  *)
  13.  
  14. function itoh(i: integer): anystring;   {integer to hex conversion}
  15.    function d(i: integer): char;
  16.    begin
  17.       i := i and 15;
  18.       if i > 9 then i := i + 7;
  19.       d := chr(i + ord('0'));
  20.    end;
  21. begin
  22.    itoh := d(i shr 12) + d(i shr 8) + d(i shr 4) + d(i);
  23. end;
  24.  
  25.  
  26. (* *)
  27. (******************************************************
  28.  *
  29.  * Procedure:  i_to_ur
  30.  *
  31.  * Purpose:    converts an unsigned integer into an unsigned real
  32.  *
  33.  * Example:    r := i_to_ur(i);
  34.  *
  35.  *)
  36.  
  37. function i_to_ur(i: integer): real;  {integer to unsigned-real conversion}
  38. var
  39.    r: real;
  40. begin
  41.    r := int( i shr 1 );
  42.    if (i and 1) <> 0 then
  43.       r := r + r + 1.0
  44.    else
  45.       r := r + r;
  46.  
  47.    i_to_ur := r;
  48. end;
  49.  
  50. (* *)
  51. (******************************************************
  52.  *
  53.  * Procedure:  ur_to_i
  54.  *
  55.  * Purpose:    converts an unsigned real into an integer
  56.  *
  57.  * Example:    i := ur_to_i(r);
  58.  *
  59.  *)
  60.  
  61. function ur_to_i(r: real): integer;  {unsigned-real to integer conversion}
  62. var
  63.    i:  integer;
  64.    r2: real;
  65. begin
  66.    r := int(r);
  67.    r2 := int(r / 2.0);
  68.    if r2+r2 = r then
  69.       i := trunc(r2) shl 1
  70.    else
  71.       i := (trunc(r2) shl 1) or 1;
  72.  
  73.    ur_to_i := i
  74. end;
  75.  
  76.  
  77.