Non-decimal radices/Convert: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: corrected input to have (at least) correct spelling. -- ~~~~ |
m →{{header|REXX}}: added/changed comments, added whitespace, eliminate a subroutine. -- ~~~~ |
||
Line 1,316: | Line 1,316: | ||
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/ |
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/ |
||
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/ |
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/ |
||
/* |
/*special chars must be viewable.*/ |
||
numeric digits 1000 /*what |
numeric digits 1000 /*what da hey, support gihugeics.*/ |
||
maxB=length(@@@) /*max base (radix) supported here*/ |
maxB=length(@@@) /*max base (radix) supported here*/ |
||
parse arg x toB inB 1 ox . /*get a number, toBase, |
parse arg x toB inB 1 ox . /*get a number, toBase, inBase*/ |
||
if toB=='' | toB==',' then toB=10 |
if toB=='' | toB==',' then toB=10 /*if skipped, assume default (10)*/ |
||
if inB=='' | inB==',' then inB=10 |
if inB=='' | inB==',' then inB=10 /* " " " " " */ |
||
if inB<2 | inb>maxB then call erb 'inBase',inB |
if inB<2 | inb>maxB then call erb 'inBase',inB /*bad boy inBase.*/ |
||
if toB<2 | tob>maxB then call erb 'toBase',toB |
if toB<2 | tob>maxB then call erb 'toBase',toB /* " " toBase.*/ |
||
if x=='' then call erm |
if x=='' then call erm /* " " number.*/ |
||
sigX=left(x,1); if pos(sigX,"-+")\==0 then x=substr(x,2) |
sigX=left(x,1); if pos(sigX,"-+")\==0 then x=substr(x,2) /*X has sign?*/ |
||
else sigX= |
else sigX= /*no sign. */ |
||
#=0; do j=1 for length(x) /*convert X, base inB ──► base 10*/ |
#=0; do j=1 for length(x) /*convert X, base inB ──► base 10*/ |
||
_=substr(x,j,1) /*pick off a "digit" from X. |
_=substr(x,j,1) /*pick off a "digit" from X. */ |
||
v=pos(_,@@@) /*get the value of this " |
v=pos(_,@@@) /*get the value of this "digit". */ |
||
if v==0 |v>inB then call erd x,j,inB |
if v==0 | v>inB then call erd x,j,inB /*illegal "digit"? */ |
||
#=#*inB+v-1 /*construct new num, dig by dig. */ |
#=#*inB+v-1 /*construct new num, dig by dig. */ |
||
end /*j*/ |
end /*j*/ |
||
y=; do while #>= |
y=; do while # >= toB /*convert #, base 10 ──► base toB*/ |
||
y=substr(@@@,(#//toB)+1,1)y /*construct the output number. */ |
y=substr(@@@,(#//toB)+1,1)y /*construct the output number. */ |
||
#=#%toB /*... and whittle # down also. */ |
#=#%toB /*... and whittle # down also. */ |
||
end /*while #>-toB*/ |
end /*while #>-toB*/ |
||
y=sigX || substr(@@@,#+1,1)y |
y=sigX || substr(@@@,#+1,1)y /*prepend the sign if it existed.*/ |
||
say ox "(base" inB')' center('is',20) y "(base" toB')' /*show & tell.*/ |
say ox "(base" inB')' center('is',20) y "(base" toB')' /*show & tell.*/ |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
/*──────────────────────────────────error subroutines───────────────────*/ |
/*──────────────────────────────────error subroutines───────────────────*/ |
||
erb: call ser; say 'illegal' arg(2) "base:" arg(1) "must be in range: 2──►" maxB |
erb: call ser; say 'illegal' arg(2) "base:" arg(1) "must be in range: 2──►" maxB |
||
erd: call ser; say 'illegal "digit" in' x":" _ |
erd: call ser; say 'illegal "digit" in' x":" _ |
||
erm: call ser; say 'no argument specified.' |
erm: call ser; say 'no argument specified.' |
||
ser: say; say '*** error! ***'; say; exit 13</lang> |
|||
ser: say; say '*** error! ***'; say; return</lang> |
|||
'''output''' when input is (maximum positive integer in a signed 32-bit word): <tt> 7fffffff , 16 </tt> |
'''output''' when input is (maximum positive integer in a signed 32-bit word): <tt> 7fffffff , 16 </tt> |
||
<pre style="overflow:scroll"> |
<pre style="overflow:scroll"> |