The Museum of HP Calculators

# Base Conversions for the HP-41CX (or C with X-functions)

This program was written by Todd Markley and is used here by permission.

This program is supplied without representation or warranty of any kind. The author and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

## Overview

This program will convert from any base up to base 36 using char's 0-9A-Z including fractional parts to the right of the ".". The alpha input of the non-dec number must include a "." even if it is a integer. Then the program will continue to ask for a base to convert the number to, up to base 36 using char's 0-9A-Z. After each entry press the Run/Stop button to continue. This program will destroy any data in the memory registers 1,2,10,11,12,13, and 14. Because it uses X-functions to work with the alpha register it can only be run on a 41CX, or 41C with X-function module. Bugs: some numbers will be rounded when changed to DEC so the result may not be exactly right for large numbers.

## Instructions

 Step Instructions Input Data/Units Keys Output Data/Units 1 Enter program 2 Enter number to convert x 3 Convert from dec to base or... XEQ TBASE TO BASE? 3A base R/S x(base) If you want to convert to another base R/S and go to step 3A 4 Convert from base to decimal XEQ FBASE FROM BASE? base R/S x(10) 5 For new case, go to step 2

## The Program

```LINE    KEYS
; TBASE - DEC TO ANY BASE
001     LBL'TBASE               ; Start of program "To BASE"
002     'NUMBER?                ; Print "NUMBER?" in A
003     AVIEW                   ; Let the user see it
004     STOP                    ; Stop program for input of number
005     STO 02                  ; Save number for data input
006     LBL'TBASE1              ; Entry from end of "From BASE"
007     'TO BASE?               ; Print "TO BASE?" in A
008     AVIEW                   ; Let the user see it
009     STOP                    ; Stop program for input of base
010     STO 01                  ; Save new base in 01
011     CLA                     ; Clear A for data out
012     RCL 02                  ; Get number in dec
013     LBL 02                  ; Loop entry for INT part of number
014     INT                     ; Take integer
015     STO 14                  ; Save
016     RCL 01                  ; Get radix
017     ÷
018     STO 14                  ; Update number
019     FRC                     ; Take frac
020     RCL 01                  ; Get radix
021     *
022     XEQ 03                  ; Call number to chr routine
023     -1                      ; To the left one
024     AROT                    ; Move number
025     RCL 14                  ; Get current number left
026     INT                     ; Take integer
027     X!=0?                   ; Is anything left?
028     GTO 02                  ; Yes, then loop back to 02
029     46                      ; "." , Start of frac segment
030     XTOA                    ; Put in A
031     RCL 02                  ; Get from number
032     LBL 04                  ; Loop entry point for frac part of number
033     FRC                     ; Take only the frac of input
034     STO 14                  ; Save
035     RCL 01                  ; Get radix
036     *
037     STO 14                  ; Update data
038     INT                     ; Int part is next char in answer
039     XEQ 03                  ; Call routine to put chr into A
040     11                      ; Put a limit of 12 chr answer
041     ALENG                   ; Test current length
042     X>Y?                    ; Is it >11 ?
043     GTO 05                  ; If yes then end
044     RCL 14                  ; Get current data
045     X!=0?                   ; Test for any left to work with
046     GTO 04                  ; Yes, then continue
047     LBL 05                  ; All done
049     STOP                    ; Stop the program
050     LBL 03                  ; Change number in X to chr and put in A
051     .5
052     +                       ; round up to
053     INT                     ; take into account the precision error
054     48                      ; Offset from zero to "0"
056     57                      ; Test for >9
057     X<>Y                    ; Swap X & Y
058     X<=Y?                   ; Compare
059     GTO 01                  ; If not >9 then continue
060     7                       ; Offset from "9" to "A"
062     LBL 01                  ; Jump point from last test
063     XTOA                    ; Put into right side of A
064     END                     ; End of tbase

; FBASE - ANY BASE TO DEC
001     LBL'FBASE               ; Start of From BASE
002     LBL'BASE                ; Alt name for base program
003     'FROM BASE?             ; Print message
004     AVIEW                   ; Let the user see it
005     STOP                    ; Stop for radix input
006     STO 01                  ; Save in mem 01
007     'NUMBER                 ; Print message
008     AON                     ; Put in alpha mode
009     STOP                    ; Stop for number input
010     AOFF                    ; Alpha off
011     ALENG                   ; Size of data
012     STO 10                  ; Save
013     1
014     -
015     STO 12                  ; Pointer to first chr
016     RCL 10                  ; Get length
017     1000                    ; Setup counter data
018     ÷
019     1
020     +
021     STO 11                  ; Save counter in 11
022     LBL 01                  ; Loop point for finding "."
023     ATOX                    ; Chr to X
024     46                      ; "."
025     X=Y?                    ; Test if chr is "."
026     GTO 03                  ; If so then done with "." find
027     X<>Y                    ; Swap X & Y
028     XTOA                    ; Put back in A
029     LBL 02                  ; Jump point
030     ISG 11                  ; Count off all chr's
031     GTO 01                  ; If more then loop
032     0
033     STO 02                  ; Clear answer reg
034     RCL 10                  ; Get length of string
035     1000                    ; Setup loop data
036     ÷
037     1
038     +
039     STO 11                  ; Save in loop counter
040     LBL 06                  ; Loop start point
041     ATOX                    ; Get first chr
042     48                      ; Zero to "0" offset
043     -                       ; Sub offset
044     STO 13                  ; Save
045     10                      ; Test for >9
046     X<=Y?                        ; Do we need to sub the "9" to "A" offset?
047     GTO 04                  ; Yes, go sub "9" to "A" offset.
048     LBL 05                  ; Re-entry point
049     RCL 13                  ; Get chr value
050     0                       ; Compare to zero
051     X>Y?                    ; Is it less then zero?
052     GTO 99                  ; Yes ERROR!
053     RCL 01                  ; Get radix
054     X<=Y?                   ; Is value outside radix range?
055     GTO 99                  ; Yes ERROR!
056     RCL 01                  ; Get radix
057     RCL 12                  ; Get power pointer
058     Y^X                     ; * factor
059     RCL 13                  ; Get value
060     *                       ; Chr's dec total value
062     1                       ; One chr down
063     ST- 12                  ; Take one away from power reg for next digit
064     ISG 11                  ; Loop for all chr's
065     GTO 06                  ; Loop pointer, if more chr's to do
066     RCL 02                  ; Get answer
067     STOP                    ; Stop and let user see DEC answer
069     LBL 04                  ; Offset routine
070     7                       ; Diff from "9" to "A"
071     ST- 13                  ; Change value of chr
072     GTO 05                  ; Go back
073     LBL 99                  ; Error routine
074     'DATA ERROR             ; Print message
075     AVIEW                   ; Let the user see message
076     STOP                    ; Stop program in ERROR
077     LBL 03
078     RCL 11
079     INT
080     2
081     -
082     STO 12
083     1
084     ST- 10
085     GTO 02
086     END                     ; End of "From BASE"
```

## Register Use

```R01  RADIX - tbase & fbase
R02  ANSWER in fbase, INPUT in tbase
R10  LENGTH OF STRING - fbase
R11  COUNTER - fbase
R12  POWER pos from "." in fbase
R13  CHR VAL temp reg in fbase
R14  WORK REG in tbase
```