You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

67 lines
1.7 KiB

  1. unit uutlConversion;
  2. { Package: Utils
  3. Prefix: utl - UTiLs
  4. Beschreibung: diese Unit stellt Methoden für Konvertierung verschiedener Datentypen zur Verfügung }
  5. {$mode objfpc}{$H+}
  6. interface
  7. uses
  8. Classes, SysUtils;
  9. function Supports(const aInstance: TObject; const aClass: TClass; out aObj): Boolean; overload;
  10. function HexToBinary(HexValue: PChar; BinValue: PByte; BinBufSize: Integer): Integer;
  11. implementation
  12. function Supports(const aInstance: TObject; const aClass: TClass; out aObj): Boolean;
  13. begin
  14. result := Assigned(aInstance) and aInstance.InheritsFrom(aClass);
  15. if result then
  16. TObject(aObj) := aInstance
  17. else
  18. TObject(aObj) := nil;
  19. end;
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. //wandelt einen Hex-String in einen Blob um
  22. //@hexvalue: Hex-String
  23. //@binvalue: Zeiger auf einen Speicherbereich
  24. //@binbufsize: maximale größe die geschrieben werden darf
  25. //@result: gelesene bytes
  26. function HexToBinary(HexValue: PChar; BinValue: PByte; BinBufSize: Integer): Integer;
  27. var i,j,h,l : integer;
  28. begin
  29. i:=binbufsize;
  30. while (i>0) do
  31. begin
  32. if hexvalue^ IN ['A'..'F','a'..'f'] then
  33. h:=((ord(hexvalue^)+9) and 15)
  34. else if hexvalue^ IN ['0'..'9'] then
  35. h:=((ord(hexvalue^)) and 15)
  36. else
  37. break;
  38. inc(hexvalue);
  39. if hexvalue^ IN ['A'..'F','a'..'f'] then
  40. l:=(ord(hexvalue^)+9) and 15
  41. else if hexvalue^ IN ['0'..'9'] then
  42. l:=(ord(hexvalue^)) and 15
  43. else
  44. break;
  45. j := l + (h shl 4);
  46. inc(hexvalue);
  47. binvalue^:=j;
  48. inc(binvalue);
  49. dec(i);
  50. end;
  51. result:=binbufsize-i;
  52. end;
  53. end.