Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
(→{{header|Perl 6}}: note that Int is a subtype of Real in Perl 6) |
(→{{header|REXX}}: added comments, added whitespace, changed some comments, added support for a leading sign. -- ~~~~) |
||
Line 1,244: | Line 1,244: | ||
Instead of writing two seperate routines, only one was written to handle both tasks. |
Instead of writing two seperate routines, only one was written to handle both tasks. |
||
<br><br>This routine was ripped out from a bigger version of mine that allowed any number as input, including decimal (or whatever base) fractions. |
<br><br>This routine was ripped out from a bigger version of mine that allowed any number as input, including decimal (or whatever base) fractions. |
||
<lang rexx>/*REXX program converts numbers from one base to another, from 2 |
<lang rexx>/*REXX program converts numbers from one base to another, from 2 ──► 90.*/ |
||
/*┌────────────────────────────────────────────────────────────────────┐ |
/*┌────────────────────────────────────────────────────────────────────┐ |
||
┌─┘ Input to this program |
┌─┘ Input to this program (bases must be positive whole numbers): └─┐ |
||
│ │ |
│ │ |
||
│ x |
│ x is required (it may have a sign). │ |
||
│ toBase the base to convert X to. │ |
│ toBase the base to convert X to. │ |
||
│ inBase the base X is expressed in. │ |
│ 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 |
└─┐ of 10 to be used. The limits of bases are: 2 ──► 90. ┌─┘ |
||
└────────────────────────────────────────────────────────────────────┘*/ |
└────────────────────────────────────────────────────────────────────┘*/ |
||
@abc='abcdefghijklmnopqrstuvwxyz' /* |
@abc='abcdefghijklmnopqrstuvwxyz' /*lowercase Latin alphabet. */ |
||
@abcU=@abc; upper @abcU |
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */ |
||
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/ |
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/ |
||
@@@=@@@'<>[]{}()?~!@#$%^&*_ |
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/ |
||
/*spec. chars should be viewable.*/ |
/*spec. chars should be viewable.*/ |
||
numeric digits 1000 /*what the hey, support gihugeics*/ |
numeric digits 1000 /*what the hey, support gihugeics*/ |
||
maxB=length(@@@) /*max base (radix) supported here*/ |
maxB=length(@@@) /*max base (radix) supported here*/ |
||
parse arg x toB inB |
parse arg x toB inB 1 ox . /*get a number, toBase, inBase */ |
||
if toB=='' | toB==',' then toB=10 /*if skipped, assume default (10)*/ |
if toB=='' | toB==',' then toB=10 /*if skipped, assume default (10)*/ |
||
if inB=='' | inB==',' then inB=10 /* " " " " " */ |
if inB=='' | inB==',' then inB=10 /* " " " " " */ |
||
Line 1,268: | Line 1,268: | ||
if toB<2 | tob>maxB then call erb 'toBase',toB /*bad boy toBase.*/ |
if toB<2 | tob>maxB then call erb 'toBase',toB /*bad boy toBase.*/ |
||
if x=='' then call erm /*bad boy number.*/ |
if x=='' then call erm /*bad boy number.*/ |
||
sigX=left(x,1); if pos(sigX,"-+")\==0 then x=substr(x,2) /*X has sign?*/ |
|||
/*──────────────────────────────────convert X from base inB ──> base 10.*/ |
|||
else sigX= /*no sign. */ |
|||
#=0 |
|||
#=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 "digits".*/ |
v=pos(_,@@@) /*get the value of this "digits".*/ |
||
if v==0 |v>inB then call erd x,j,inB /*illegal "digit"? */ |
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 |
end /*j*/ |
||
/*──────────────────────────────────convert # from base 10 ──> base toB.*/ |
|||
y=; do while #>=toB /*convert #, base 10 ──► base toB*/ |
|||
y='' |
|||
y=substr(@@@,(#//toB)+1,1)y /*construct the output number. */ |
|||
y=substr(@@@,(#//toB)+1,1)y /*construnct the output number. */ |
|||
#=#%toB /*... and whittle # down also. */ |
#=#%toB /*... and whittle # down also. */ |
||
end |
end /*while #>-toB*/ |
||
y=substr(@@@,#+1,1)y |
y=sigX || substr(@@@,#+1,1)y |
||
say |
say ox "(base" inB')' center('is',20) y "(base" toB')' /*show & tell.*/ |
||
exit /*stick a fork in it, we're done.*/ |
|||
exit |
|||
/*──────────────────────────────────error subroutines───────────────────*/ |
/*──────────────────────────────────error subroutines───────────────────*/ |
||
erb: call ser; say 'illegal' arg(2) "base:" arg(1) "must be in range: 2──►" maxB |
|||
erd: call ser; say 'illegal "digit" in' x":" _; call erx |
erd: call ser; say 'illegal "digit" in' x":" _; call erx |
||
erm: call ser; say 'no argument specified.'; call erx |
|||
erx: say; exit 13 |
|||
erb: call ser; say 'illegal' arg(2) "base:" arg(1) "must be in range: 2──>" maxB |
|||
ser: say; say '*** error! ***'; say; return</lang> |
|||
'''output''' when input is (maximum positive integer in a 32-bit word): <tt> 7fffffff </tt> |
'''output''' when input is (maximum positive integer in a signed 32-bit word): <tt> 7fffffff , 16 </tt> |
||
<pre style=" |
<pre style="overflow:scroll"> |
||
7fffffff (base 16) is 2147483647 (base 10) |
7fffffff (base 16) is 2147483647 (base 10) |
||
</pre> |
</pre> |
||
'''output''' when input is: <tt> 4095 2 </tt> |
'''output''' when input is: <tt> 4095 2 </tt> |
||
<pre style=" |
<pre style="overflow:scroll"> |
||
4095 (base 10) is 111111111111 (base 2) |
4095 (base 10) is 111111111111 (base 2) |
||
</pre> |
</pre> |
||
'''output''' when input is: <tt> 100 3 2 </tt> |
'''output''' when input is: <tt> 100 3 2 </tt> |
||
<pre style=" |
<pre style="overflow:scroll"> |
||
100 (base |
100 (base 2) is 11 (base 3) |
||
</pre> |
</pre> |
||
'''output''' when input is: <tt> zombieseatingdeadvegatables 10 36 </tt> |
'''output''' when input is: <tt> zombieseatingdeadvegatables 10 36 </tt> |
||
<pre style=" |
<pre style="overflow:scroll"> |
||
zombieseatingdeadvegatbles (base 36) is 28842619147225718605337632731806510179876 (base 10) |
zombieseatingdeadvegatbles (base 36) is 28842619147225718605337632731806510179876 (base 10) |
||
</pre> |
</pre> |