Post Reply 
(28/48/50) Base Conversion Programs
01-02-2023, 07:21 PM (This post was last modified: 02-14-2023 05:48 PM by John Keith.)
Post: #1
(28/48/50) Base Conversion Programs
These two simple programs convert integers to and from lists of digits in any base. The integers, the base and the digits may be positive or negative.

The first program takes an integer on level 2 and an integer base on level 1 and returns the digits as a list. I call the program I→DL to avoid conflict with the similar ListExt command I→BL. In exact mode on the HP 49 and 50, the size of the integer and the base are limited only by memory.

Important note: If the base is positive, the level 2 integer must be positive, otherwise the program will crash with an Insufficient Memory error.

Code:

\<< DUP ABS SWAP I\->R 0. <
    DEPTH 3. - \-> b f d           @ b is absolute value of base
  \<<                              @ f is flag for negative b
    WHILE b IDIV2 DEPTH d - ROLLD  @ Move remainder to top
      DUP                          @ While quotient not 0
    REPEAT f :: NEG IFT            @ Negate if base is negative
    END DROP DEPTH d - \->LIST     @ Drop 0 and make list
  \>>
\>>

Also an approximate version for the HP-28 and 48.

Code:

\<< DUP ABS SWAP 0 < DEPTH 3 - \-> b f d
  \<<
    WHILE b DUP2 / FLOOR ROT ROT MOD  @ Replacement for IDIV2
      DEPTH d - ROLLD DUP
    REPEAT f
      \<< NEG
      \>> IFT                         @ Must use program for HP-28
    END DROP DEPTH d - \->LIST
  \>>
\>>

The next program takes a list of integers on level 2 and the base on level 1 and returns an integer.
The base may be negative or complex, and on the 49 and 50 it can be symbolic- given 'X' as the base it will return a polynomial in X. It runs without modification on any RPL calculator. Called DL→I for reason given above.

Code:

\<< \-> b
  \<< LIST\->
    IF DUP 1. >           @ Do nothing if size = 1
    THEN 1 SWAP           @ Multiplier starts with b^0 = 1
      2. SWAP             @ Start with second digit
      START b *           @ Multiply by base
        ROT OVER *        @ Multiply next digit
        ROT + SWAP        @ Add product to total
      NEXT
    END DROP              @ Drop multiplier
  \>>
\>>

A shorter version for HP 48G and later:

Code:

\<< \-> b
  \<< 0 SWAP +       @ To prevent error if list size = 1
    \<< SWAP b * +
    \>> STREAM
  \>>
\>>

Updated 1/03/2023 with improved I→DL programs.
Updated 1/04/2023 with additional note and program.
Find all posts by this user
Quote this message in a reply
02-14-2023, 05:54 PM
Post: #2
RE: (28/48/50) Base Conversion Programs
Important update fixing a bug where DL→I would crash if the level 1 list had only 1 object. Also changed OBJ→ to LIST→ for HP-28 compatibility.
Find all posts by this user
Quote this message in a reply
02-15-2023, 03:58 AM (This post was last modified: 02-15-2023 03:59 AM by Gerald H.)
Post: #3
RE: (28/48/50) Base Conversion Programs
This programme also does the job

https://www.hpmuseum.org/forum/thread-40...light=XdYB

For input

75
1
11

the programme returns

11: {6 9 "."}

For

-75
1
11

returns

11: {-6 -9 "."}

For

75
1
-11

returns

-11: {-6 9 "."}

& for

-75
1
-11

returns

-11: {6 -9 "."}
Find all posts by this user
Quote this message in a reply
Post Reply 




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