util.inc 1.7 KB

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