(* 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) *)
program RadixFun;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
  DIGITS = '0123456789ABCDEF';

Function FromStr(s : string; radix : integer) : integer;

var
  res, i : integer;

begin
  res := 0;
  for i := 1 to length(s) do begin
    res := res * radix + (Pos(s[i], DIGITS) - 1);
  end;
  FromStr := res;
end;

Function FromBinStr(s : string) : integer;

begin
  FromBinStr := FromStr(s, 2);
end;

Function FromOctStr(s : string) : integer;

begin
  FromOctStr := FromStr(s, 8);
end;

Function FromDecStr(s : string) : integer;

begin
  FromDecStr := FromStr(s, 10);
end;

Function FromHexStr(s : string) : integer;

begin
  FromHexStr := FromStr(s, 16);
end;

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

var
  tmp : integer;
  res : string;

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

Function ToBinStr(v : integer) : string;

begin
  ToBinStr := ToStr(v, 2);
end;

Function ToOctStr(v : integer) : string;

begin
  ToOctStr := ToStr(v, 8);
end;

Function ToDecStr(v : integer) : string;

begin
  ToDecStr := ToStr(v, 10);
end;

Function ToHexStr(v : integer) : string;

begin
  ToHexStr := ToStr(v, 16);
end;

begin
  writeln(FromBinStr('1010'));
  writeln(FromOctStr('12'));
  writeln(FromDecStr('10'));
  writeln(FromHexStr('A'));
  writeln(ToBinStr(10));
  writeln(ToOctStr(10));
  writeln(ToDecStr(10));
  writeln(ToHexStr(10));
end.
