Anonymous user
Non-decimal radices/Convert: Difference between revisions
→{{header|REXX}}: separated doc from program, added more error checking, added better error messages, add/changed comments, expanded support for larger numbers.
(→{{header|Ruby}}: Added example) |
(→{{header|REXX}}: separated doc from program, added more error checking, added better error messages, add/changed comments, expanded support for larger numbers.) |
||
Line 1,703:
=={{header|REXX}}==
Instead of writing two seperate routines, only one was written to handle both tasks.
<lang rexx>/*REXX program converts numbers from one base to another, from 2 ──► 90.*/▼
▲/*┌────────────────────────────────────────────────────────────────────┐
▲┌─┘ Input to this program (bases must be positive whole numbers): └─┐
│ │
│
│
│
│ If X has a leading sign, it is maintained (kept) after conversion. │
│ │
│ toBase or inBase can be a comma (,) which causes the default │
└─┐
└────────────────────────────────────────────────────────────────────┘
▲<lang rexx>/*REXX
@abc = 'abcdefghijklmnopqrstuvwxyz'
@@ = 0123456789 || @abc || @abcU /*prefix 'em with numeric digits.*/
@@
/*special chars must be viewable.*/▼
if
if
if
if inB=='' | inB=="," then inB=10 /* " " " " " */
if inB<2 | inb>maxB | \datatype(inB,'W') then call erb 'inBase ' inB
▲ else sigX= /*no sign. */
if toB<2 | toB>maxB | \datatype(toB,'W') then call erb 'toBase ' toB
#=0
#=#*inB+_-1 /*build a new number, dig by dig.*/
y=
do
y=substr(@@, (#//toB)+1,
#=#%toB /*··· and whittle # down also. */
say ox "(base" inB')' center('is',20) y "(base" toB')' /*show & tell.*/▼
y=sigX || substr(@@, #+1, 1)y /*prepend the sign if it existed.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────one─liner subroutines───────────────*/
erb: call ser
erd: call ser
erm: call ser
ser: say; say '***
'''output''' when input is (maximum positive integer in a signed 32-bit word): <tt> 7fffffff , 16 </tt>
<pre>
|