util.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by Michael Van Canneyt and Florian Klaempfl
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. Function IntToStr (I : Longint) : String;
  12. begin
  13. Str(I,Result);
  14. end;
  15. function IsValidIdent(const Ident: string): Boolean;
  16. begin
  17. Result:=True;
  18. end;
  19. procedure BinToHex(BinValue, HexValue: PChar; BinBufSize: Integer);
  20. Const
  21. HexDigits='0123456789ABCDEF';
  22. var
  23. i : longint;
  24. begin
  25. for i:=0 to binbufsize-1 do
  26. begin
  27. HexValue[0]:=hexdigits[1+((ord(binvalue^) shr 4))];
  28. HexValue[1]:=hexdigits[1+((ord(binvalue^) and 15))];
  29. inc(hexvalue,2);
  30. inc(binvalue);
  31. end;
  32. end;
  33. function HexToBin(HexValue, BinValue: PChar; BinBufSize: Integer): Integer;
  34. // more complex, have to accept more than bintohex
  35. // A..F 1000001
  36. // a..f 1100001
  37. // 0..9 110000
  38. var i,j,h,l : integer;
  39. begin
  40. i:=binbufsize;
  41. while (i>0) do
  42. begin
  43. if hexvalue^ IN ['A'..'F','a'..'f'] then
  44. h:=((ord(hexvalue^)+9) and 15)
  45. else if hexvalue^ IN ['0'..'9'] then
  46. h:=((ord(hexvalue^)) and 15)
  47. else
  48. break;
  49. inc(hexvalue);
  50. if hexvalue^ IN ['A'..'F','a'..'f'] then
  51. l:=(ord(hexvalue^)+9) and 15
  52. else if hexvalue^ IN ['0'..'9'] then
  53. l:=(ord(hexvalue^)) and 15
  54. else
  55. break;
  56. j := l + (h shl 4);
  57. inc(hexvalue);
  58. binvalue^:=chr(j);
  59. inc(binvalue);
  60. dec(i);
  61. end;
  62. result:=binbufsize-i;
  63. end;
  64. {
  65. $Log$
  66. Revision 1.4 2005-04-14 17:43:53 michael
  67. + Fix for BintoHex and hextobin by Uberto Barbini
  68. Revision 1.3 2005/02/14 17:13:31 peter
  69. * truncate log
  70. Revision 1.2 2005/02/03 20:17:05 florian
  71. + BinToHex and HexToBin from Marco added
  72. }