Tuesday, May 9, 2017

Lazarus String To Hex and vice versa

function StringToHex(S: String): string;
function HexToString(H: String): String;



function StringToHex(S: String): string;
var I: Integer;
begin
  Result:= '';
  for I := 1 to length (S) do
    Result:= Result+IntToHex(ord(S[i]),2);
end;

function HexToString(H: String): String;
var I: Integer;
begin
  Result:= '';
  for I := 1 to length (H) div 2 do
    Result:= Result+Char(StrToInt('$'+Copy(H,(I-1)*2+1,2)));
end;




procedure TForm1.Button6Click(Sender: TObject);
var a,b: string;
begin
 a := Edit1.Text;
 b := StringToHex(a);
 Edit2.Text := b;
end;

procedure TForm1.Button7Click(Sender: TObject);
var a,b: string;
begin
 a := Edit1.Text;
 b := HexToString(a);
 Edit2.Text := b;

end;