unit uutlConversion; { Package: Utils Prefix: utl - UTiLs Beschreibung: diese Unit stellt Methoden für Konvertierung verschiedener Datentypen zur Verfügung } {$mode objfpc}{$H+} interface uses Classes, SysUtils; function Supports(const aInstance: TObject; const aClass: TClass; out aObj): Boolean; overload; function HexToBinary(HexValue: PChar; BinValue: PByte; BinBufSize: Integer): Integer; implementation function Supports(const aInstance: TObject; const aClass: TClass; out aObj): Boolean; begin result := Assigned(aInstance) and aInstance.InheritsFrom(aClass); if result then TObject(aObj) := aInstance else TObject(aObj) := nil; end; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //wandelt einen Hex-String in einen Blob um //@hexvalue: Hex-String //@binvalue: Zeiger auf einen Speicherbereich //@binbufsize: maximale größe die geschrieben werden darf //@result: gelesene bytes function HexToBinary(HexValue: PChar; BinValue: PByte; BinBufSize: Integer): Integer; var i,j,h,l : integer; begin i:=binbufsize; while (i>0) do begin if hexvalue^ IN ['A'..'F','a'..'f'] then h:=((ord(hexvalue^)+9) and 15) else if hexvalue^ IN ['0'..'9'] then h:=((ord(hexvalue^)) and 15) else break; inc(hexvalue); if hexvalue^ IN ['A'..'F','a'..'f'] then l:=(ord(hexvalue^)+9) and 15 else if hexvalue^ IN ['0'..'9'] then l:=(ord(hexvalue^)) and 15 else break; j := l + (h shl 4); inc(hexvalue); binvalue^:=j; inc(binvalue); dec(i); end; result:=binbufsize-i; end; end.