home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH01 / LIBRARY / CONVERTS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-05  |  9.8 KB  |  329 lines

  1. unit Converts;
  2. interface
  3.  
  4. uses
  5.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  6.   Forms, Dialogs, StdCtrls, ExtCtrls;
  7.  
  8. function CheckHex(const hex:string):boolean;
  9. function CheckDec(const dec:string):boolean;
  10. function CheckUnsigned(const dec:string):boolean;
  11. function CheckBin(const bin:string):boolean;
  12. function BinToInt(const bin:string):integer;
  13. function IntToBin(Value:LongInt; Digits:cardinal):TCaption;
  14. function IntToHex(Value:LongInt; Digits:cardinal):TCaption;
  15. function HexToInt(const hex:string):integer;
  16.  
  17.  
  18.  
  19.  
  20. implementation
  21. { The following function returns true if the string parameter contains }
  22. { a legal representation of a 16-bit hexadecimal value.                }
  23. { Keep in mind that "Result" is where Delphi functions return their    }
  24. { function result.                                                     }
  25.  
  26. function CheckHex(const hex:string):boolean;
  27. const
  28.      HexVals = ['0'..'9', 'A'..'F', 'a'..'f'];
  29. var
  30.      index:integer;
  31.      len:  integer;
  32. begin
  33.  
  34.      len := length(hex);
  35.      Result := (len <> 0) and (len <= 4);
  36.      for index := 1 to len do begin
  37.  
  38.          Result := Result and (hex[index] in HexVals);
  39.  
  40.      end;
  41. end;
  42.  
  43.  
  44.  
  45.  
  46.  
  47. { The following function checks its parameter to see if it is a string }
  48. { that properly represents a signed decimal value.                     }
  49.  
  50. function CheckDec(const dec:string):boolean;
  51. const
  52.      DecVals = ['0'..'9'];
  53. var
  54.      index:integer;
  55.      len:  integer;
  56.      Start:integer;
  57. begin
  58.  
  59.      len := length(dec);
  60.  
  61.      { First, check for total bail-out conditions; this would be any value }
  62.      { greater than +32767 or less than -32768; a string that is just too  }
  63.      { long, a zero length string, or a string containing only the '-'.    }
  64.  
  65.      Result := false;
  66.      Start := 1;
  67.      if (len = 0) then exit;
  68.      if (dec[1] = '-') then begin
  69.  
  70.         if (len = 1) then exit;
  71.         if (len > 6) then exit;
  72.         if (len = 6) and (dec > '-32768') then exit;
  73.  
  74.         Start := 2;  { Skip over '-' when testing characters }
  75.  
  76.      end
  77.      else begin
  78.  
  79.           if (len >= 6) then exit;
  80.           if (len = 5) and (dec > '32767') then exit;
  81.  
  82.      end;
  83.  
  84.  
  85.  
  86.      { Okay, if the length is five or six, it is not a numeric value that  }
  87.      { is out of range.  However, the string could still contain illegal   }
  88.      { characters.  We'll check for that down here.                        }
  89.  
  90.      Result := true;
  91.      for index := Start to len do
  92.          Result := Result and (dec[index] in DecVals);
  93. end;
  94.  
  95.  
  96.  
  97.  
  98. { CheckUnsigned is the same operation as CheckDec except it does not have  }
  99. { to worry about negative numbers.                                         }
  100.  
  101. function CheckUnsigned(const dec:string):boolean;
  102. const
  103.      DecVals = ['0'..'9'];
  104. var
  105.      index:integer;
  106.      len:  integer;
  107. begin
  108.  
  109.      len := length(dec);
  110.  
  111.      { Check for totally illegal values here }
  112.  
  113.      Result := false;
  114.      if (len = 0) then exit;
  115.      if (len >= 6) then exit;
  116.      if (len = 5) and (dec > '65535') then exit;
  117.  
  118.  
  119.      { If the tests above succeeded, check the individual characters here. }
  120.  
  121.      Result := true;
  122.      for index := 1 to len do
  123.          Result := Result and (dec[index] in DecVals);
  124. end;
  125.  
  126.  
  127.  
  128.  
  129. { CheckBin checks the "bin" string to see if it is a valid binary number.  }
  130. { This particular function allows spaces in binary numbers;  this lets the }
  131. { use separate nibbles in a long binary string for readability.  Note that }
  132. { this function allows spaces to occur in arbitrary places in the string,  }
  133. { it simply ignores the spaces.                                            }
  134.  
  135. function CheckBin(const bin:string):boolean;
  136. const
  137.      BinVals = ['0','1',' '];
  138. var
  139.      index:integer;
  140.      len:  integer;
  141.      Bits: integer;
  142. begin
  143.  
  144.      len := length(bin);
  145.  
  146.      { if the string's length is zero or greater than 19 (16 digits plus   }
  147.      { three spaces to separate the nibbles) then immediately return an    }
  148.      { error.                                                              }
  149.  
  150.      Result := (len <> 0) and (len <= 19);
  151.      if (not Result) then exit;
  152.  
  153.      { If the length of the string is okay, then check each character in   }
  154.      { the string to make sure it is a zero, one, or a space.              }
  155.  
  156.      Bits := 0;
  157.      for index := 1 to len do begin
  158.  
  159.          Result := Result and (bin[index] in BinVals);
  160.          Bits := Bits + ord((bin[index] = '0') or (bin[index] = '1'));
  161.  
  162.      end;
  163.      Result := Result and (Bits <=16) and (Bits > 0);
  164. end;
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. { IntToBin-                                                           }
  172. {                                                                     }
  173. {    This function converts an integer value to a string of zeros and }
  174. {    ones -- the binary representation of that integer.  The integer  }
  175. {    to convert is the first parameter passed to this function.  The  }
  176. {    second parameter specifies the number of digits to place in the  }
  177. {    output string (maximum 16);  If the Digits value is less than    }
  178. {    the number of bits actually present in the first parameter, this }
  179. {    function outputs the L.O. 'Digits' bits.  Note that this routine }
  180. {    inserts space between each group of four digits in order to make }
  181. {    the output more readable.                                        }
  182.  
  183. function IntToBin(
  184.                     Value:LongInt;
  185.                     Digits:cardinal
  186.                  ):TCaption;
  187. var
  188.    Mask: cardinal;
  189.    Count:integer;
  190. begin
  191.  
  192.  
  193.      { Result will hold the string this function produces }
  194.  
  195.      Result := '';
  196.  
  197.      { Create a mask value with a single bit in the H.O. bit position }
  198.      { so we can use it to see if the current value contains a zero   }
  199.      { or one in its H.O. bit.                                        }
  200.  
  201.      Mask := $8000;
  202.  
  203.      { Eliminate the bits we're not interested in outputting.  This   }
  204.      { adjusts the value so the first bit we want to test is located  }
  205.      { in bit #15 of Value.                                           }
  206.  
  207.      Value := Value shl (16-Digits);
  208.  
  209.      { For each of the bits we want in the output string, test the    }
  210.      { current value to see if it's H.O. bit is zero or one.  Append  }
  211.      { the corresponding character to the end of the result string.   }
  212.      { If the bit position is an even multiple of four, append a      }
  213.      { a space as well, although this code will not append a space to }
  214.      { the end of the string.                                         }
  215.  
  216.      Count := 16-Digits;        {# of bits we're going to test.       }
  217.  
  218.      While (true) do begin      {Really a loop..endloop construct.    }
  219.  
  220.          if ((Value and Mask) <> 0) then
  221.               AppendStr(Result, '1')
  222.          else AppendStr(Result,'0');
  223.  
  224.          inc(Count);
  225.          if (Count > 15) then break;
  226.  
  227.          { After bits 3, 7, and 11, append a space to the string.     }
  228.  
  229.          if (Count mod 4) = 0 then AppendStr(Result,' ');
  230.  
  231.          { Adjust the Mask for the next loop iteration.  This moves   }
  232.          { the single one bit in Mask one position to the right so it }
  233.          { tests the next lower bit in Value.                         }
  234.  
  235.          Mask := Mask shr 1;
  236.  
  237.      end;
  238. end;
  239.  
  240.  
  241.  
  242.  
  243.  
  244. { IntToHex-                                                           }
  245. {                                                                     }
  246. {    This function converts an integer value to a string of hex       }
  247. {    characters.  The integer to convert is the first parameter.      }
  248. {    The second parameter specifies the number of digits to place in  }
  249. {    the output string (maximum 4);  If the Digits value is less than }
  250. {    the number of bits actually present in the first parameter, this }
  251. {    function outputs the L.O. digits.                                }
  252.  
  253. function IntToHex(Value:LongInt; Digits:cardinal):TCaption;
  254. const
  255.      HexChars:array [0..$f] of char =
  256.                             ('0','1','2','3','4','5','6','7','8','9',
  257.                              'A','B','C','D','E','F');
  258. var
  259.    Count:integer;
  260. begin
  261.  
  262.  
  263.      { Result will hold the string this function produces }
  264.  
  265.      Result := '';
  266.  
  267.  
  268.      { For each of the nibbles we want to output, append the corres-   }
  269.      { ponding hex digit.                                              }
  270.  
  271.      for Count := Digits-1 downto 0 do begin
  272.  
  273.          AppendStr(Result, HexChars[ (Value shr (Count*4)) and $f]);
  274.  
  275.      end;
  276. end;
  277.  
  278.  
  279.  
  280.  
  281.  
  282. { HexToInt-                                                           }
  283. {                                                                     }
  284. { This routine converts a string of characters that represent a hexa- }
  285. { decimal value into the corresponding integer value.  This routine   }
  286. { assumes that the string is a valid representation of a hexadecimal  }
  287. { value.  One should call "CheckHex" prior to this routine if not     }
  288. { absolutely sure that the string is valid.                           }
  289.  
  290. function HexToInt(const hex:string):integer;
  291. var
  292.    index:integer;
  293. begin
  294.  
  295.      Result := StrToInt('$'+hex);
  296.  
  297. end;
  298.  
  299.  
  300.  
  301. { BinToInt-                                                           }
  302. {                                                                     }
  303. { The following routine converts a string containing a sequence of    }
  304. { ones and zeros (i.e., a binary number representation) into an inte- }
  305. { ger value.  Note that this routine ignores any spaces appearing in  }
  306. { the binary string;  this allows the user to place spaces in the     }
  307. { binary number to make it more readable.                             }
  308.  
  309. function BinToInt(const bin:string):integer;
  310. var
  311.    index:integer;
  312. begin
  313.  
  314.      Result := 0;
  315.      for index := 1 to length(bin) do
  316.          if bin[index] <> ' ' then
  317.             Result := (Result shl 1) + (ord(bin[index]) and $1);
  318.  
  319. end;
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328. end.
  329.