home *** CD-ROM | disk | FTP | other *** search
- # ANYBASE.TPI
- # by Kent Peterson 10/24/93
-
- defstr digit$
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- digit$ store
-
- defvar frombase
- defvar tobase
- defstr numin$
-
- define ** ( x y -- x**y )
- # raises x to the y power
- # Note: only works with non-negative
- # y values
- dup
- case 0 of drop drop 1 endof
- 1 of drop endof
- default
- over swap
- 1 - do over * loop
- swap drop
- endcase
- enddef
-
- begin
- "Convert number" print$
- get$ ucase$ dup$ numin$ store
- "" =$ if bye endif
- "from base" print$
- getnum frombase store
- "to base" print$
- getnum tobase store
-
- # Convert the number to base 10
- 0
- numin$ fetch len drop$
- do
- digit$ fetch
- index 1 numin$ fetch mid$
- instr 1 -
- drop$ drop$
- numin$ fetch len drop$
- index - frombase fetch swap **
- dup 0 = if drop 1 endif
- * +
- loop
-
- # Convert the number from base 10
- # to the target base
- ""
- begin
- dup tobase fetch mod
- 1 + digit$ fetch 1 mid$ swap$ +$
- tobase fetch / dup not
- until drop
-
- print$ cr
- 0 until
-