Jump to content

Non-decimal radices/Convert: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace, used templates for the output sections.
(Added Wren)
m (→‎{{header|REXX}}: added/changed whitespace, used templates for the output sections.)
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.
┌────────────────────────────────────────────────────────────────────┐
<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 toBase is required the (itbase mayto haveconvert a sign) X to.
toBase the base toinBase convert the base X is expressed toin. │
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). */
@abc = 'abcdefghijklmnopqrstuvwxyz' /*lowercase (Latin or English) alphabet*/
Line 2,693 ⟶ 2,691:
/* [↑] all characters must be viewable*/
numeric digits 3000 /*what da hey, support gihugeic numbers*/
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 ··· */
if pos(sigX, "+-")\==0 then x=x2 x= x2 /*does X have a leading sign (+ or -) ?*/
else sigX= /*Nope. No leading sign for the X value*/
if x=='' then call erm /*if no X number, issue an error msg.*/
if toB=='' | toB=="," then toB=10 10 /*if skipped, assume the default (10). */
if inB=='' | inB=="," then inB=10 10 /* " " " " " " */
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
#=0 /*result of converted X (in 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. */
_= pos(?, @@) /*calculate the value of this numeral. */
if _==0 | _>inB then call erd x /*is _ character an illegal numeral? */
#= # * inB+_-1 + _ - 1 /*build a new number, digit by digit. */
end /*j*/ /* [↑] this also verifies digits. */
y= /*the value of X in base B. */
do while # >= toB /*convert #: base 10 ──► base toB.*/
y= substr(@@, (#//toB) + 1, 1)y /*construct the output number. */
#= # %toB toB /* ··· and whittle # down also. */
end /*while*/ /* [↑] algorithm may leave a residual.*/
/* [↓] Y is the residual. */
y= sigX || substr(@@, #+1, 1)y /*prepend the sign if it existed. */
say ox "(base" inB')' center("is", 20) y '(base' toB")"
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,723 ⟶ 2,721:
erm: call ser 'no argument specified.'
ser: say; say '***error!***'; say arg(1); exit 13</lang>
'''{{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>
7fffffff (base 16) is 2147483647 (base 10)
</pre>
'''{{out|output''' |text=&nbsp; when input used (expressed in decimal) is: &nbsp; &nbsp; <tt> 4095 &nbsp; 2 </tt>}}
<pre>
4095 (base 10) is 111111111111 (base 2)
</pre>
'''{{out|output''' |text=&nbsp; when input used (expressed in decimalbinary) is: &nbsp; &nbsp; <tt> 100 &nbsp; 3 &nbsp; 2 </tt>}}
<pre>
100 (base 2) is 11 (base 3)
</pre>
'''{{out|output''' |text=&nbsp; when input used (expressed in base 3662) is: &nbsp; &nbsp; <tt> zombieseatingdeadvegetableszombiesAreEatingDeadVegetables &nbsp; 10 &nbsp; 3662 </tt>}}
<pre>
zombieseatingdeadvegetableszombiesAreEatingDeadVegetables (base 3662) is 1038334289300125869792154778345043071467300337500751396688020801073824403268172711989016896916476 (base 10)
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.