Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!adm!xadmx!R1TMARG%AKRONVM.BITNET@cornellc.cit.cornell.edu From: R1TMARG%AKRONVM.BITNET@cornellc.cit.cornell.edu (Tim Margush) Newsgroups: comp.lang.pascal Subject: Brain Teaser (number conversion) Message-ID: <21181@adm.BRL.MIL> Date: 18 Oct 89 19:46:34 GMT Sender: news@adm.BRL.MIL Lines: 102 Included is a routine to convert from one base to another. This submission does not first convert to base ten. It also is not limited by the size of an integer variable. I wrote this in VS-Pascal and converted it to turbo syntax (strings are different) so there may be a few problems. I am not sure how turbo handles concatenation of strings and characters for example. -----------------------------cut here--------------------------------- program con; {written by Tim Margush, University of Akron, 10/18/1989} {converts base b1 to base b2} { 1 < b1,b2 < 37, length of numbers limited to string lengths} {legal digits are 0..9, A..Z (up to limit of b1-1)} var a,b,c:string; b1,b2:integer; function c2int(x:char):integer; begin {returns integer for character digit} if (x>='0') and (x<='9') then c2int:=ord(x)-ord('0') else c2int:=ord(x)-ord('A')+10 end; function int2c(x:integer):string; begin {returns character digit for integer} if x<10 then int2c:=str(chr(ord('0')+x)) else int2c:=str(chr(ord('A')+x-10)) end; function sum(x:string;y,b:integer):string; {computes the sum of x and y in base b} var carry,digit,s:integer; sm:string; begin carry:=y;sm:=''; for digit:=length(x) downto 1 do begin s:=c2int(x[digit]) + carry; sm:=concat(int2c(s mod b),sm); carry:=s div b end; while carry>0 do begin sm:=concat(int2c(carry mod b),sm); carry:=carry div b end; sum:=sm end; function product(x:string;y,b:integer):string; {computes the product of x and y in base b} var carry,digit,p:integer; prod:string; begin carry:=0;prod:=''; for digit:=length(x) downto 1 do begin p:=c2int(x[digit]) * y + carry; prod:=concat(int2c(p mod b),prod); carry:=p div b end; while carry>0 do begin prod:=concat(int2c(carry mod b),prod); carry:=carry div b end; product:=prod end; function convert(in_base,out_base:integer;in_num:string):string; {convert base in_base numeral to out_base numeral using the definition of place value representation. All arithmetic is done in base out_base. In_num is processed from left to right.} var digit:integer; out_num:string; begin out_num:='0'; for digit:=1 to length(in_num) do out_num:=sum(product(out_num,in_base,out_base), c2int(in_num[digit]),out_base); convert:=out_num end; begin {test driver} repeat writeln('enter oldbase newbase numeral'); readln(b1,b2,a); writeln('original:',a); c:=convert(b1,b2,a); writeln('converted to base ',b2:1,':',c) until a='0' end. -----------------------------and here-------------------------------- Tim Margush R1TMARG@AKRONVM.BITNET Department of Mathematical Sciences R1TMARG@VM1.CC.UAKRON.EDU University Of Akron R1TMARG@AKRONVM.UAKRON.EDU Akron, OH 44325 (216) 375-7109