unit OBDBytes;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, StrUtils;

type
  TfrmOBDBytes = class(TForm)
    Label1: TLabel;
    edtHEX: TEdit;
    btnOK: TButton;
    btnCancel: TButton;
  private
    { Private declarations }
  public
    function Execute(var sz: string): Boolean;
  end;

var
  frmOBDBytes: TfrmOBDBytes;

implementation

{$R *.DFM}

function StringToValue(sz: string): string;
var
  i: Integer;
begin
  Result := '';
  for i := 1 to Length(sz) do
  begin
    Result := Result + Format('%.2x', [Byte(sz[i])]);
  end;
end;

function ValueToString(sz: string): string;
var
  i: Integer;
  szHEX: string;
begin
  Result := '';
  szHEX := '';
  for i := 1 to Length(sz) do
  begin
    if sz[i] = ' ' then
      Continue;
    szHEX := szHEX + sz[i];
    if Length(szHEX) = 2 then
    begin
      Result := Result + Char(Hex2Dec(szHEX));
      szHEX := '';
    end;
  end;
end;

function TfrmOBDBytes.Execute(var sz: string): Boolean;
begin
  edtHEX.Text := StringToValue(sz);
  if ShowModal = mrOK then
  begin
    sz := ValueToString(edtHEX.Text);
  end;
  Result := True;
end;

end.
