Anonymous user
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 ''number-conversion'' BIFs (<u>B</u>uilt-<u>I</u>n <u>F</u>unctions) were used in this REXX program.
┌────────────────────────────────────────────────────────────────────┐▼
┌─┘ Input to this program (bases must be positive integers > 1): └─┐▼
▲ ┌────────────────────────────────────────────────────────────────────┐
│ │▼
▲┌─┘ Input to this program (bases must be positive integers > 1): └─┐
│
│ If X has a leading sign, it is maintained (kept) after conversion. │▼
▲│ │
│ │
▲│ If X has a leading sign, it is maintained (kept) after conversion. │
└─┐ of 10 to be used. The limits of bases are: 2 ──► 90. ┌─┘▼
└────────────────────────────────────────────────────────────────────┘▼
▲└─┐ of 10 to be used. The limits of bases are: 2 ──► 90. ┌─┘
▲ └────────────────────────────────────────────────────────────────────┘
<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(@@)
parse arg x toB inB 1 ox . 1 sigX 2 x2 . /*obtain: three args, origX, sign ··· */
if pos(sigX, "+-")\==0 then
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=
if inB=='' | inB=="," then 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
#=0 /*result of converted X (in base 10).*/
do j=1 for length(x) /*convert X: base inB ──► base 10. */
?= substr(x,j,1)
_= pos(?, @@)
if _==0 | _>inB then call erd x /*is _ character an illegal numeral? */
#= # * inB
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
#= # %
end /*while*/ /* [↑] algorithm may leave a residual.*/
/* [↓] Y is the residual. */
y= sigX || substr(@@, #+1, 1)y
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>
<pre>
7fffffff (base 16) is 2147483647 (base 10)
</pre>
<pre>
4095 (base 10) is 111111111111 (base 2)
</pre>
<pre>
100 (base 2) is 11 (base 3)
</pre>
<pre>
</pre>
|