util.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. {%MainUnit classes.pp}
  2. {
  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. procedure BinToHex(BinValue, HexValue: PAnsiChar; BinBufSize: Integer);
  12. Const
  13. HexDigits='0123456789ABCDEF';
  14. var
  15. i : longint;
  16. begin
  17. for i:=0 to binbufsize-1 do
  18. begin
  19. HexValue[0]:=hexdigits[1+((ord(binvalue^) shr 4))];
  20. HexValue[1]:=hexdigits[1+((ord(binvalue^) and 15))];
  21. inc(hexvalue,2);
  22. inc(binvalue);
  23. end;
  24. end;
  25. function HexToBin(HexValue, BinValue: PAnsiChar; BinBufSize: Integer): Integer;
  26. // more complex, have to accept more than bintohex
  27. // A..F 1000001
  28. // a..f 1100001
  29. // 0..9 110000
  30. var i,j,h,l : integer;
  31. begin
  32. i:=binbufsize;
  33. while (i>0) do
  34. begin
  35. if hexvalue^ IN ['A'..'F','a'..'f'] then
  36. h:=((ord(hexvalue^)+9) and 15)
  37. else if hexvalue^ IN ['0'..'9'] then
  38. h:=((ord(hexvalue^)) and 15)
  39. else
  40. break;
  41. inc(hexvalue);
  42. if hexvalue^ IN ['A'..'F','a'..'f'] then
  43. l:=(ord(hexvalue^)+9) and 15
  44. else if hexvalue^ IN ['0'..'9'] then
  45. l:=(ord(hexvalue^)) and 15
  46. else
  47. break;
  48. j := l + (h shl 4);
  49. inc(hexvalue);
  50. binvalue^:=chr(j);
  51. inc(binvalue);
  52. dec(i);
  53. end;
  54. result:=binbufsize-i;
  55. end;