home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 5 / ctrom5b.zip / ctrom5b / PROGRAM / DIVERSEN / TIPI2A / ANYBASE.TPI < prev    next >
Text File  |  1994-10-02  |  1KB  |  60 lines

  1. # ANYBASE.TPI by Kent Peterson 10/24/93
  2.  
  3. defstr digit$
  4. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  5. digit$ store
  6.  
  7. defvar frombase
  8. defvar tobase
  9. defstr numin$
  10.  
  11. define ** ( x y -- x**y )
  12. # raises x to the y power
  13. # Note: only works with non-negative
  14. # y values
  15.  dup
  16.  case 0 of drop drop 1 endof
  17.       1 of drop endof
  18.  default
  19.       over swap
  20.       1 - do over * loop
  21.      swap drop
  22.  endcase
  23. enddef
  24.  
  25. begin
  26. |
  27. | Note: hitting [Enter] alone on the next line will exit.
  28.  "Convert number" print$
  29.  get$ ucase$ dup$ numin$ store
  30.  "" =$ if bye endif
  31.  "from base" print$
  32.  getnum frombase store
  33.  "to base" print$
  34.  getnum tobase store
  35.  
  36. # Convert the number to base 10
  37.  0
  38.  numin$ fetch len drop$
  39.  do
  40.   digit$ fetch
  41.   index 1 numin$ fetch mid$
  42.   instr 1 -
  43.   drop$ drop$
  44.   numin$ fetch len drop$
  45.   index - frombase fetch swap **
  46.   dup 0 = if drop 1 endif
  47.   * +
  48.  loop
  49.  
  50. # Convert the number from base 10
  51. # to the target base
  52.  ""
  53.  begin
  54.   dup tobase fetch mod
  55.   1 + digit$ fetch 1 mid$ swap$ +$
  56.   tobase fetch / dup not
  57.  until drop
  58.  print$ cr
  59. 0 until
  60.