From: Daniel Lidstrom <danli97@ite.mh.se>
Newsgroups: comp.sys.hp48
Subject: Converting between bases
Date: Wed, 18 Nov 1998 20:26:02 GMT
Organization: Deja News - The Leader in Internet Discussion
Lines: 65
Message-ID: <72vag3$m4i$1@nnrp1.dejanews.com>
NNTP-Posting-Host: 193.10.251.97
X-Article-Creation-Date: Wed Nov 18 20:26:02 1998 GMT
X-Http-User-Agent: Mozilla/4.06 [en] (WinNT; I)
X-Http-Proxy: 1.0 x2.dejanews.com:80 (Squid/1.1.22) for client 193.10.251.97
Xref: republic.btigate.com comp.sys.hp48:18742

Here are two programs that convert between bases. They go well together as one
can use the others output as input and vice versa.

Description
-----------
BASEN: Converts a decimal based number into a base of your choice
Syntax: num base -> ""
A string is used as there can be letters in the answer.

->DEC: Converts a number in a base other than decimal to decimal.
Syntax: "num" base -> num

Examples: 17131711 28 BASEN -> 17131711(28): "ROBIN"
          "DANIEL" 30 ->DEC -> 324637641
          123 2 BASEN -> 123(2): "1111011"
          "F" 16 ->DEC -> 15
          .1 2 BASEN -> (interrupted) ".00011001100110011...."
          .375 2 BASEN -> .375(2): ".011"
          .333333333333 3 BASEN -> ...: ".1"

BASEN handles fractions as well. ->DEC does not. That is left as an exercise
for you!

BASEN
Bytes: 244 #B4EEh
<< OVER IP -> b n
  << ""
    WHILE n
    REPEAT n b MOD
       IF DUP 9 >
       THEN 55 + CHR
       END SWAP + n b / IP 'n' STO
    END
    IF OVER FP
    THEN "." +
    END OVER
    WHILE FP DUP
    REPEAT b * 11 RND DUP IP
ROT SWAP + SWAP
    END DROP SWAP "(" b + ")" + +
->TAG
   >>
>>


->DEC
Bytes: 144 # 9E7h
<< -> b
   << DTAG 0 SWAP
      WHILE DUP SIZE
      REPEAT DUP HEAD
NUM
      IF 65 OVER <=
      THEN 55
      ELSE 48
      END - OVER
SIZE 1 - b SWAP ^ *
ROT + SWAP TAIL
      END
   >> DROP
>>
