(* Collection of code snippets by Arne Vajhøj *)
(* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) *)
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

const
  valid = '0123456789ABCDEF';

function FromRadix(s : string; radix : integer) : integer;

var
  i,res : integer;

begin
  res := 0;
  for i := 1 to length(s) do begin
    res := radix * res + pos(s[i], valid) - 1;
  end;
  FromRadix := res;
end;

function ToRadix(v : integer; radix : integer) : string;

var
  tmp : integer;
  res : string;

begin
  res := '';
  tmp := v;
  while tmp>0 do begin
    res := valid[tmp mod radix + 1] + res;
    tmp := tmp div radix;
  end;
  ToRadix := res;
end;

var
  v : integer;

begin
  case ComboBox1.ItemIndex of
    0 : v := FromRadix(Edit1.Text, 2);
    1 : v := FromRadix(Edit1.Text, 10);
    2 : v := FromRadix(Edit1.Text, 16);
  end;
  case ComboBox2.ItemIndex of
    0 : Edit2.Text := ToRadix(v, 2);
    1 : Edit2.Text := ToRadix(v, 10);
    2 : Edit2.Text := ToRadix(v, 16);
  end;
end;

end.
