'********************************************** '* This program converts a number in base a * '* into a number in base b (a and b must be * '* between 2 and 36). * '* ------------------------------------------ * '* Ref.: "Mathematiques en Turbo-Pascal by * '* M. Ducamp and A. Reverchon (2), * '* Eyrolles, Paris, 1988" [BIBLI 05]. * '* ------------------------------------------ * '* Sample runs: * '* * '* BASE CONVERSION * '* * '* Start base (2 to 36): 5 * '* Arival base (2 to 36): 3 * '* * '* Enter number in start base: 3421 * '* In base 3: 200000 * '* Enter number in start base: 3420 * '* In base 3: 122222 * '* * '* * '* BASE CONVERSION * '* * '* Start base (2 to 36): 10 * '* Arival base (2 to 36): 16 * '* * '* Enter number in start base: 65535 * '* In base 3: FFFF * '* Enter number in start base: 100 * '* In base 3: 64 * '* * '* BASE CONVERSION * '* * '* Start base (2 to 36): 16 * '* Arival base (2 to 36): 2 * '* * '* Enter number in start base: FF * '* In base 3: 11111111 * '* Enter number in start base: 3A * '* In base 3: 111010 * '* Enter number in start base: E2 * '* In base 3: 11100010 * '* * '* Note: Enter null string to exit. * '* * '* Basic Release by J-P Moreau * '* (www.jpmoreau.fr) * '********************************************** 'Explanations: '------------ 'The letters A,B,...,Z represent 10,11,...,36 'in the base > 10. 'The number is first converted from base a to 'base 10 by Function Decodebase, then converted 'from base 10 to base b by Function Codebase. '----------------------------------------------- defint i-n XMAXREAL = 1E30 ' iba,ibd: INTEGER ' x$,y$: STRING ' r: REAL 'main program cls print print " BASE CONVERSION" print input " Start base (2 to 36): ", ibd input " Arival base (2 to 36): ", iba 10 print input " Enter number in start base: ", x$ if x$<>"" then gosub 1000 'Decodebase(x$,ibd,r) if IERROR=0 then gosub 2000 'Codebase(r,iba,y$) if IERROR=0 then print " In base "; iba; ": "; y$ else print " Error in coding number." end if else print " Errorin decoding number." end if end if if x$<>"" then goto 10 print END 'Convert a number x$ from base ibd to base 10. The subroutine set 'IERROR to 1 if ibd not in [2..36] or if string x$ contains invalid 'characters in base ibd or if result r is too big, else IERROR=0. 'Subroutine Decodebase(x$,ibd,r) 'xmult: REAL 'ch$:CHAR 1000 IERROR=1 if ibd<2 or ibd>36 then print " base must be between 2 and 36 !" return end if r=0 : xmult=1 long=len(x$) for i=1 to long ch$=mid$(x$,long+1-i,1) if ch$<"0" or ch$>"Z" or (ch$>"9" and ch$<"A") then return if ch$<="9" then j=asc(ch$)-asc("0") else j=asc(ch$)-asc("A")+10 end if if j>=ibd then return r=r+xmult*j if xmult>XMAXREAL/ibd then return xmult=xmult*ibd next i IERROR=0 Return 'Convert a number r from base 10 to base iba. The subroutine set 'IERROR to 1 if iba not in [2..36], else IERROR=0. 'Subroutine Codebase(r,iba,y$) 2000 IERROR=1 if iba<2 or iba>36 then print " base must be between 2 and 36 !" return end if y$="": x=r 2010 if x<=0 then goto 2020 n=INT(x-iba*INT(x/iba)) if n<10 then y$=Chr$(Asc("0")+n)+y$ else y$=Chr$(Asc("A")+n-10)+y$ end if x=INT(x/iba) goto 2010 2020 IERROR=0 Return 'end of file basis.bas