function HexToBin(HexNr: String): String;
Var
Counter: integer;
Function HexCharToInt(HexToken: char): Integer;
Begin
If HexToken > #97 Then
HexToken := Chr(Ord(HexToken) - 32);
{ use lowercase aswell }
Result := 0;
If (HexToken > #47) And (HexToken < #58) Then { chars 0....9 }
Result := Ord(HexToken) - 48
Else If (HexToken > #64) And (HexToken < #71) Then { chars A....F }
Result := Ord(HexToken) - 65 + 10;
End;
Function HexCharToBin(HexToken: char): String;
Var
DivLeft: integer;
Begin
DivLeft := HexCharToInt(HexToken); { first HEX->BIN }
Result := '';
{ Use reverse dividing }
Repeat { Trick; divide by 2 }
If odd(DivLeft) Then { result = odd ? then bit = 1 }
Result := '1' + Result { result = even ? then bit = 0 }
Else
Result := '0' + Result;
DivLeft := DivLeft Div 2; { keep dividing till 0 left and length = 4 }
Until (DivLeft = 0) And (length(Result) = 4); { 1 token = nibble = 4 bits }
End;
Begin
Result := '';
For Counter := 1 To length(HexNr) Do
Result := Result + HexCharToBin(HexNr[Counter]);
End;