Non-decimal radices/Convert: Difference between revisions

m
→‎{{header|REXX}}: added REXX language.
m (→‎{{header|REXX}}: added REXX language.)
Line 919:
s = baseN(k,16) # returns the string 1a
i = int('1a',16) # returns the integer 26</lang>
 
=={{header|REXX}}==
Instead of writing two seperate routines, only one was written to handle both tasks.
<lang rexx>
/*REXX program converts numbers from one base to another, from 2 ──> 62.*/
 
/*┌────────────────────────────────────────────────────────────────────┐
┌─┘ Input to this program (all must be positive whole numbers): └─┐
│ │
│ x (is required). │
│ toBase the base to convert X to. │
│ inBase the base X is expressed in. │
│ │
│ toBase or inBase can be a comma (,) which causes the default │
└─┐ of 10 to be used. The limits of both are: 2 ──> 62. ┌─┘
└────────────────────────────────────────────────────────────────────┘*/
 
@abc='abcdefghijklmnopqrstuvwxyz' /*random characters, sorted. */
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/
numeric digits 1000 /*what the hey, support biggies. */
maxB=length(@@@) /*max base (radix) supported here*/
parse arg x toB inB . /*get a number, toBase, inBase */
if toB=='' | toB=='.' then toB=10 /*if skipped, assume default (10)*/
if inB=='' | inB=='.' then inB=10 /* " " " " " */
if inB<2 | inb>maxB then call erb 'inBase',inB /*bad boy inBase.*/
if toB<2 | tob>maxB then call erb 'toBase',toB /*bad boy toBase.*/
if x=='' then call erm /*bad boy number.*/
 
/*──────────────────────────────────convert X from base inB ──> base 10.*/
#=0
do j=1 for length(x)
_=substr(x,j,1) /*pick off a "digit" from X. */
v=pos(_,@@@) /*get the value of this "digits".*/
if v==0 |v>inB then call erd x,j,inB /*illegal "digit"? */
#=#*inB+v-1 /*construct new num, dig by dig. */
end
 
/*──────────────────────────────────convert # from base 10 ──> base toB.*/
y=''
do while #>=toB /*deconstruct the new number (#).*/
y=substr(@@@,(#//toB)+1,1)y /*construnct the output number. */
#=#%toB /*... and whittle # down also. */
end
 
y=substr(@@@,#+1,1)y
say x "(base" inB')' center('is',20) y "(base" toB')' /*show & tell.*/
exit
 
 
/*──────────────────────────────────error subroutines───────────────────*/
erm: call ser; say 'no argument specified.'; call erx
erd: call ser; say 'illegal "digit" in' x":" _; call erx
ser: say; say '*** error! ***'; say; return
erb: call ser; say 'illegal' arg(2) "base:" arg(1) "must be in range: 2──>" maxB
erx: say; exit 13
</lang>
Output when input is (maximum positive integer in a 32-bit word):
<br><br>
7fffffff
<pre style="height:5ex;overflow:scroll">
7fffffff (base 16) is 2147483647 (base 10)
</pre>
Output when input is:
<br><br>
4095 2
<pre style="height:5ex;overflow:scroll">
4095 (base 10) is 111111111111 (base 2)
</pre>
Output when input is:
<br><br>
100 3 2
<pre style="height:5ex;overflow:scroll">
100 (base 10) is 10201 (base 3)
</pre>
Output when input is:
<br><br>
zombieseatingdeadvegatables 10 36
<pre style="height:5ex;overflow:scroll">
zombieseatingdeadvegatbles (base 36) is 28842619147225718605337632731806510179876 (base 10)
</pre>
 
=={{header|Ruby}}==