/* REXX procedure CVTBASEN.CMD -- converts a decimal number to another base */ /* Copyright Dave Schnee, 1994 */ say "How many decimal digits should I use for all computations? (5 to 1000)" do while \(datatype(numd, 'Whole number') = 1) pull numd if (numd="QUIT") then exit if \(datatype(numd, 'Whole number') = 1) then do numd='x' say " (enter a decimal integer from 5 to 1000, please)" end else if (numd < 5) | (numd > 1000) then do numd='x' say " (enter a decimal integer from 5 to 1000, please)" end end /* do */ numeric digits numd say "What number base do you wish to use? (1 to 91)" do while \(datatype(base, 'Number') = 1) pull base if (base="QUIT") then exit if \(datatype(base, 'Number') = 1) then do base = 'x' say " (you must enter a number for the base)" end else if (base < 1) | (base = 1) | (base > 91) then do base = 'x' say " (please enter a number >1 and <=91 for the base)" end end /* do */ numalph="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()~`-=_+|}{][';/:?><" say " digits = " numd " base = " base if (base>36) then do say " The digits to be used are: " substr(numalph, 1, 45) say " " substr(numalph, 46) end do FOREVER keyinput='x' say "Enter the input number in decimal (>0) or QUIT to end" do while \(datatype(keyinput, 'Number') = 1) pull keyinput if (keyinput="QUIT") then exit if \(datatype(keyinput, 'Number') = 1) then do keyinput = 'x' say " (you must enter a number for the conversion)" end else if (keyinput < 0) | (keyinput = 0) then do keyinput = 'x' say " (please enter a number > zero for the conversion)" end end /* do */ /* Now convert "keyinput" from decimal to "base" using "digits" of precision */ /* Step 1 - find the maximum number of (output) digits needed to the left of the radix point - the power of "base" */ maxpower = 0 /* assume "units" position is maximum */ dvalue = 1 do while \((keyinput % dvalue) < base ) maxpower = maxpower + 1 dvalue = dvalue * base end /* do */ maxout = 100 say " maxout = " maxout " input = " keyinput outlen = 0 output = "" /* Steps 2==>N, peel off the next higher digit (for the correct radix), reduce the remaining number by that digit times the current power of the radix. continue until either the remaining number is zero or our patience is exhausted. */ do while ( ((keyinput > 0) | (maxpower > -1)) & (outlen < maxout) ) outdig = keyinput % dvalue outlen = outlen + 1 output = insert(substr(numalph,(outdig+1),1), output, outlen) if maxpower = 0 then do outlen = outlen + 1 output = insert('.', output, outlen) end keyinput = keyinput - (dvalue * outdig) dvalue = dvalue / base maxpower = maxpower - 1 end /* do */ say "The result is: " output end /* do */