Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
(Added Wren) |
m (→{{header|REXX}}: added/changed whitespace, used templates for the output sections.) |
||
Line 2,672: | Line 2,672: | ||
No ''number-conversion'' BIFs (<u>B</u>uilt-<u>I</u>n <u>F</u>unctions) were used in this REXX program. |
No ''number-conversion'' BIFs (<u>B</u>uilt-<u>I</u>n <u>F</u>unctions) were used in this REXX program. |
||
⚫ | |||
<pre> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
│ x is required (it may have a sign). │ |
|||
│ 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 │ |
|||
⚫ | |||
│ toBase or inBase can be a comma (,) which causes the default │ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
</pre> |
|||
<lang rexx>/*REXX program converts integers from one base to another (using bases 2 ──► 90). */ |
<lang rexx>/*REXX program converts integers from one base to another (using bases 2 ──► 90). */ |
||
@abc = 'abcdefghijklmnopqrstuvwxyz' /*lowercase (Latin or English) alphabet*/ |
@abc = 'abcdefghijklmnopqrstuvwxyz' /*lowercase (Latin or English) alphabet*/ |
||
Line 2,693: | Line 2,691: | ||
/* [↑] all characters must be viewable*/ |
/* [↑] all characters must be viewable*/ |
||
numeric digits 3000 /*what da hey, support gihugeic numbers*/ |
numeric digits 3000 /*what da hey, support gihugeic numbers*/ |
||
maxB=length(@@) |
maxB= length(@@) /*max base/radix supported in this code*/ |
||
parse arg x toB inB 1 ox . 1 sigX 2 x2 . /*obtain: three args, origX, sign ··· */ |
parse arg x toB inB 1 ox . 1 sigX 2 x2 . /*obtain: three args, origX, sign ··· */ |
||
if pos(sigX, "+-")\==0 then |
if pos(sigX, "+-")\==0 then x= x2 /*does X have a leading sign (+ or -) ?*/ |
||
else sigX= /*Nope. No leading sign for the X value*/ |
else sigX= /*Nope. No leading sign for the X value*/ |
||
if x=='' then call erm /*if no X number, issue an error msg.*/ |
if x=='' then call erm /*if no X number, issue an error msg.*/ |
||
if toB=='' | toB=="," then toB= |
if toB=='' | toB=="," then toB= 10 /*if skipped, assume the default (10). */ |
||
if inB=='' | inB=="," then inB= |
if inB=='' | inB=="," then inB= 10 /* " " " " " " */ |
||
if inB<2 | inB>maxB | \datatype(inB,'W') then call erb "inBase " inB |
if inB<2 | inB>maxB | \datatype(inB, 'W') then call erb "inBase " inB |
||
if toB<2 | toB>maxB | \datatype(toB,'W') then call erb "toBase " toB |
if toB<2 | toB>maxB | \datatype(toB, 'W') then call erb "toBase " toB |
||
#=0 /*result of converted X (in base 10).*/ |
#=0 /*result of converted X (in base 10).*/ |
||
do j=1 for length(x) /*convert X: base inB ──► base 10. */ |
do j=1 for length(x) /*convert X: base inB ──► base 10. */ |
||
?=substr(x,j,1) |
?= substr(x,j,1) /*pick off a numeral/digit from X. */ |
||
_=pos(?, @@) |
_= pos(?, @@) /*calculate the value of this numeral. */ |
||
if _==0 | _>inB then call erd x /*is _ character an illegal numeral? */ |
if _==0 | _>inB then call erd x /*is _ character an illegal numeral? */ |
||
#=#*inB |
#= # * inB + _ - 1 /*build a new number, digit by digit. */ |
||
end /*j*/ /* [↑] this also verifies digits. */ |
end /*j*/ /* [↑] this also verifies digits. */ |
||
y= /*the value of X in base B. */ |
y= /*the value of X in base B. */ |
||
do while # >= toB /*convert #: base 10 ──► base toB.*/ |
do while # >= toB /*convert #: base 10 ──► base toB.*/ |
||
y=substr(@@, (#//toB)+1, 1)y |
y= substr(@@, (#//toB) + 1, 1)y /*construct the output number. */ |
||
#=#% |
#= # % toB /* ··· and whittle # down also. */ |
||
end /*while*/ /* [↑] algorithm may leave a residual.*/ |
end /*while*/ /* [↑] algorithm may leave a residual.*/ |
||
/* [↓] Y is the residual. */ |
/* [↓] Y is the residual. */ |
||
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")" |
say ox "(base" inB')' center("is", 20) y '(base' toB")" |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Line 2,723: | Line 2,721: | ||
erm: call ser 'no argument specified.' |
erm: call ser 'no argument specified.' |
||
ser: say; say '***error!***'; say arg(1); exit 13</lang> |
ser: say; say '***error!***'; say arg(1); exit 13</lang> |
||
{{out|output|text= when input is expressed in hexadecimal (maximum positive integer in a signed 32-bit word): <tt> 7fffffff , 16 </tt>}} |
|||
<pre> |
<pre> |
||
7fffffff (base 16) is 2147483647 (base 10) |
7fffffff (base 16) is 2147483647 (base 10) |
||
</pre> |
</pre> |
||
{{out|output|text= when input used (expressed in decimal) is: <tt> 4095 2 </tt>}} |
|||
<pre> |
<pre> |
||
4095 (base 10) is 111111111111 (base 2) |
4095 (base 10) is 111111111111 (base 2) |
||
</pre> |
</pre> |
||
{{out|output|text= when input used (expressed in binary) is: <tt> 100 3 2 </tt>}} |
|||
<pre> |
<pre> |
||
100 (base 2) is 11 (base 3) |
100 (base 2) is 11 (base 3) |
||
</pre> |
</pre> |
||
{{out|output|text= when input used (expressed in base 62) is: <tt> zombiesAreEatingDeadVegetables 10 62 </tt>}} |
|||
<pre> |
<pre> |
||
zombiesAreEatingDeadVegetables (base 62) is 337500751396688020801073824403268172711989016896916476 (base 10) |
|||
</pre> |
</pre> |
||