Post Reply 
(49G) Convert Integer from Integer Base A to Integer Base B
07-15-2022, 10:27 AM (This post was last modified: 10-03-2022 12:58 AM by Gene.)
Post: #1
(49G) Convert Integer from Integer Base A to Integer Base B
Programme inspired by the programmes here:

https://www.hpmuseum.org/forum/thread-17734.html

Enter the number to be converted as a list of digits in base ten form for increasing powers of base, starting with units at the right of the list & highest power of base leftmost
then
the original base
then
the base converting to.

eg The stack

{ 17 56 9797 4 }

18975263

18975262

will convert the level three base 18975263 number to the base 18975262 number

{ 17 107 9960 9874 }.


Code:
CKSUM # 5897h

BYTES 96.5

« → A B
  « REVLIST OBJ→ 0 1. ROT
    START A * +
    NEXT { } SWAP
    DO B IDIV2 ROT + SWAP DUP NOT
    UNTIL
    END DROP
  »
»
Find all posts by this user
Quote this message in a reply
07-15-2022, 01:09 PM (This post was last modified: 10-02-2022 08:31 PM by John Keith.)
Post: #2
RE: HP 49G: Convert Integer from Integer Base A to Integer Base B
Very nice program, thanks for posting.

It could be used on older calculators for numbers < 10^12 if IDIV2 is replaced by DUP2 / FLOOR ROT ROT MOD.

For users of the ListExt Library, a much shorter equivalent:

Code:

\<< UNROT BL\->I SWAP I\->BL
\>>
Find all posts by this user
Quote this message in a reply
07-15-2022, 06:45 PM
Post: #3
RE: HP 49G: Convert Integer from Integer Base A to Integer Base B
(07-15-2022 10:27 AM)Gerald H Wrote:  eg The stack

{ 17 56 9797 4 }

18975263

18975262

will convert the level three base 18975263 number to the base 18975262 number

{ 17 107 9960 9874 }.

We can go from base b1 to base b2, by synthetic division.

b2 = b1 - 1

1 > 17 56 9797 4
1 > 17 73 9870 9874
1 > 17 90 9960
1 > 17 107

Results might require doing carries (in base b2), to limit digits = 0 .. b2-1

This avoided the steps of getting N, and the reverse of N to base b2.

N = horner([17, 56, 9797, 4], b1) = 116148179948924836904678
Find all posts by this user
Quote this message in a reply
07-16-2022, 09:07 AM
Post: #4
RE: HP 49G: Convert Integer from Integer Base A to Integer Base B
Here a sys version of the programme in posting #1:

Code:
Size: 99.5

CkSum: # 2568h

::
  CK3&Dispatch
  # 5FFFF
  ::
    2NULLLAM{}_
    BIND
    INNERCOMP
    reversym
    ZINT 0
    SWAP
    ZERO_DO
    OVER
    TYPEZINT?
    NcaseTYPEERR
    2GETLAM
    FPTR2 ^RMULText
    FPTR2 ^RADDext
    LOOP
    NULL{}
    SWAP
    BEGIN
    1GETLAM
    FPTR2 ^ZDIVext
    ROT
    SWAP>HCOMP_
    SWAP
    FPTR2 ^DupQIsZero?
    UNTIL
    DROP
    ABND
  ;
;
Find all posts by this user
Quote this message in a reply
07-16-2022, 12:54 PM (This post was last modified: 08-20-2022 04:31 AM by Gerald H.)
Post: #5
RE: HP 49G: Convert Integer from Integer Base A to Integer Base B
Following John's suggestion in Post #2 the programme looks like this:

Code:
CKSUM # E678h

BYTES 103.5

« → A B
  « REVLIST OBJ→ 0. 1. ROT
    START A * +
    NEXT { } SWAP
    DO B DUP2 / IP UNROT MOD ROT + SWAP DUP NOT
    UNTIL
    END DROP
  »
»

This now permits the use of real numbers as a base, eg

For stack

{ 7. 8. 9. 7. 8. 9. }

10.

1.41421356237

the programme returns

{ 1. .58578643763 1.17157287526 1.34314575052 1.10050506341 .44365081393 .37258300208 .47308806549 .33095244179 .53196256861 .24769132121 .64971157485 .08116908005 .88520958733 .74812459773 .94199204992 .66782207072 .0555569751 .92143057907 .69690038783 .0144340334 .97958721329 .61465450443 1.13074730184 1.40088188412 .01885391657 .9733366435 .62349427077 1.11824616226 .4185614168 1.40806519978 1.00869570886 .57348971245 1.18896429298 .31855230016 .54950146122 .22289103795 }

On entering the two stack levels

1.41421356237

10.

the programme then returns

{ 7. 8. 9. 7. 8. 9. }
Find all posts by this user
Quote this message in a reply
10-02-2022, 08:38 PM
Post: #6
RE: HP 49G: Convert Integer from Integer Base A to Integer Base B
On further testing, I have determined that the correct equivalent of IDIV2 is DUP2 / FLOOR ROT ROT MOD. Using IP gives incorrect result if either integer is negative. I have updated my original post, in which I also neglected the IP (now FLOOR).
Find all posts by this user
Quote this message in a reply
01-06-2023, 07:02 PM
Post: #7
RE: (49G) Convert Integer from Integer Base A to Integer Base B
A modification of the program in post #1 which allows negative bases. The first half looks much shorter but actually takes the same number of bytes as the first half of the original program.

Code:

\<< DUP ABS 1 ROT 0 < :: NEG IFT \-> a b f
  \<<                     @ f = -1 if base is negative else 1
    \<< SWAP a * +
    \>> STREAM { } SWAP
    DO b IDIV2 ROT + SWAP
      f * DUP NOT         @ Negate quotient if base is negative
    UNTIL
    END DROP
  \>>
\>>

Also an approximate number version which is larger but runs on any RPL calculator. The first half is similar to my program DL→I in this thread.

Code:

\<< DUP ABS 1 ROT 0 <
  \<< NEG
  \>> IFT \-> a b f                           @ Must use program for HP-28
  \<< OBJ\-> 1 SWAP 2. SWAP
    START a * ROT OVER * ROT + SWAP
    NEXT DROP { } SWAP                        @ No REVLIST or STREAM
    DO b DUP2 / FLOOR ROT ROT MOD ROT + SWAP  @ Nor UNROT
     f * DUP NOT
    UNTIL
    END DROP
  \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)