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 &nbsp; ''number-conversion'' &nbsp; BIFs &nbsp; (<u>B</u>uilt-<u>I</u>n <u>F</u>unctions) &nbsp; were used in this REXX program.
No &nbsp; ''number-conversion'' &nbsp; BIFs &nbsp; (<u>B</u>uilt-<u>I</u>n <u>F</u>unctions) &nbsp; were used in this REXX program.
┌────────────────────────────────────────────────────────────────────┐
<pre>
┌─┘ Input to this program (bases must be positive integers > 1): └─┐
┌────────────────────────────────────────────────────────────────────┐
│ │
┌─┘ Input to this program (bases must be positive integers > 1): └─┐
x is required (it may have a sign).
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.
│ If X has a leading sign, it is maintained (kept) after conversion. │
│ │
│ │
│ If X has a leading sign, it is maintained (kept) after conversion. │
toBase or inBase can be a comma (,) which causes the default
└─┐ of 10 to be used. The limits of bases are: 2 ──► 90. ┌─┘
│ toBase or inBase can be a comma (,) which causes the default │
└────────────────────────────────────────────────────────────────────┘
└─┐ of 10 to be used. The limits of bases are: 2 ──► 90. ┌─┘
└────────────────────────────────────────────────────────────────────┘
</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(@@) /*max base/radix supported in this code*/
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 x=x2 /*does X have a leading sign (+ or -) ?*/
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=10 /*if skipped, assume the default (10). */
if toB=='' | toB=="," then toB= 10 /*if skipped, assume the default (10). */
if inB=='' | inB=="," then inB=10 /* " " " " " " */
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) /*pick off a numeral/digit from X. */
?= substr(x,j,1) /*pick off a numeral/digit from X. */
_=pos(?, @@) /*calculate the value of this numeral. */
_= 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+_-1 /*build a new number, digit by digit. */
#= # * 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 /*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*/ /* [↑] 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 /*prepend the sign if it existed. */
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>
'''output''' &nbsp; when input is expressed in hexadecimal &nbsp; (maximum positive integer in a signed 32-bit word): &nbsp; <tt> 7fffffff &nbsp; , &nbsp; 16 </tt>
{{out|output|text=&nbsp; when input is expressed in hexadecimal &nbsp; (maximum positive integer in a signed 32-bit word): &nbsp; &nbsp; <tt> 7fffffff &nbsp; , &nbsp; 16 </tt>}}
<pre>
<pre>
7fffffff (base 16) is 2147483647 (base 10)
7fffffff (base 16) is 2147483647 (base 10)
</pre>
</pre>
'''output''' &nbsp; when input used (expressed in decimal) is: &nbsp; <tt> 4095 &nbsp; 2 </tt>
{{out|output|text=&nbsp; when input used (expressed in decimal) is: &nbsp; &nbsp; <tt> 4095 &nbsp; 2 </tt>}}
<pre>
<pre>
4095 (base 10) is 111111111111 (base 2)
4095 (base 10) is 111111111111 (base 2)
</pre>
</pre>
'''output''' &nbsp; when input used (expressed in decimal) is: &nbsp; <tt> 100 &nbsp; 3 &nbsp; 2 </tt>
{{out|output|text=&nbsp; when input used (expressed in binary) is: &nbsp; &nbsp; <tt> 100 &nbsp; 3 &nbsp; 2 </tt>}}
<pre>
<pre>
100 (base 2) is 11 (base 3)
100 (base 2) is 11 (base 3)
</pre>
</pre>
'''output''' &nbsp; when input used (expressed in base 36) is: &nbsp; <tt> zombieseatingdeadvegetables &nbsp; 10 &nbsp; 36 </tt>
{{out|output|text=&nbsp; when input used (expressed in base 62) is: &nbsp; &nbsp; <tt> zombiesAreEatingDeadVegetables &nbsp; 10 &nbsp; 62 </tt>}}
<pre>
<pre>
zombieseatingdeadvegetables (base 36) is 1038334289300125869792154778345043071467300 (base 10)
zombiesAreEatingDeadVegetables (base 62) is 337500751396688020801073824403268172711989016896916476 (base 10)
</pre>
</pre>