home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
MBUG
/
MBUG039.ARC
/
CONVERT.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1979-12-31
|
4KB
|
158 lines
(**from Colin Mc Carthy date 17/11/84
The following program written in Turbo Pascal
will convert from any base to another.
For example hex to another bases, type
$3C00 <RETURN>**)
(** Program convert dec/hex/octal/binary **)
var c,a,h,o,b,x:string[32];
tsave,num,num1,r,k,t,nn:real;
i,n,result:integer;
error,err,start,flag:boolean;
q:array[1..20] of real;
p:array[1..20] of char;
ident:char;
procedure errors;
begin
write(^g,^g,^g,'Input error! ');
delay(1300);
error:=true;
flag:=true;
err:=false;
a:='';
end;
procedure cstring;
begin
a:='';
for i:=1 to n do
begin
if q[i]>9 then c:=chr(trunc(q[i])+55)
else str(trunc(q[i]),c);
a:=a+' '+c;
end;
end;
procedure def;
begin
q[i]:=int(t/nn);
t:=t-int(t/nn)*nn;
end;
procedure calc;
var dm:string[32];
aa,xp,m:integer;
l:real;
begin
t:=0;
a:=copy(a,2,length(a));
xp:=length(a)-1;l:=r;
for i:=1 to length(a) do
begin
if xp=0 then r:=1;
for m:=1 to xp-1 do r:=r*l;
dm:=copy(a,i,1);
if dm>=upcase('A') then str(ord(upcase(dm))-55,dm);
val(dm,aa,result);
t:=t+aa*r;xp:=xp-1;r:=l;
end;
end;
begin
start:=true;
while start do begin
error:=true;
err:=false;
while error do
begin
ident:=' ';
flag:=false;
clrscr;
writeln('This program converts numbers into other bases.');
writeln('Input your numbers in the following form:- <RETURN> to end.');
writeln;
writeln('<Decimal> or <-Decimal>, <$Hexadecimal>, <#Octal>, <%Binary>');
writeln;
readln(a);
if a='' then halt;
val(a,k,result);
if result=1 then k:=0;
if not flag then
begin
if abs(k)>65535.0 then errors;
if result>1 then errors;
if copy(a,1,1)>'9' then errors;
end;
if copy(a,1,1)='$' then
begin
if length(a)>5 then errors;
for i:=1 to length(a) do
begin
if copy(a,i+1,1)>'Z' then
if copy(a,i+1,1)>'f' then err:=true;
if copy(a,i+1,1)<'Z' then
if copy(a,i+1,1)>'F' then err:=true;
end;
end;
if err then errors;
val(copy(a,2,length(a)),num,result);
if a<>'' then ident:=copy(a,1,1);
if (ident='#') then
begin
if num>177777.0 then errors;
for i:=2 to length(a) do if copy(a,i,1)>'0' then errors;
end;
if ident='%' then
begin
if length(a)>17 then errors;
for i:=2 to length(a) do if copy(a,i,1)>'1' then errors;
end;
if not flag then error:=false;
val(a,t,result);
end;
if ident='#' then
begin
r:=8;calc;
end;
if ident='%' then
begin
r:=2;calc;
end;
if ident='$' then
begin
r:=16;calc;
end;
if t<0 then t:=65536.0+t;tsave:=t;
nn:=65536.0;
for i:=1 to 3 do
begin
nn:=nn/16;def;
end;
q[4]:=t;n:=4;
cstring;h:=a;t:=tsave;
nn:=262144.0;
for i:=1 to 5 do
begin
nn:=nn/8;def;
end;
q[6]:=t;o:='';n:=6;cstring;
o:=a;t:=tsave;
nn:=65536.0;
for i:=1 to 15 do
begin
nn:=nn/2;def;
end;
q[16]:=t;n:=16;cstring;b:=a;
clrscr;
writeln('Decimal=');
writeln(tsave:5:0,' (',tsave-65536.0:5:0,')');
writeln;
writeln('Hexadecimal=');
writeln(h);
writeln;
writeln('Octal=');
writeln(o);
writeln;
writeln('Binary=');
writeln(b);
writeln;writeln;
write('Press <RETURN> ');
repeat until keypressed;
end;(* start *)
end.