Back to the index

Base Converter for the HP-32SII

Store an integer between 2 and 16 in variable B. Routine T converts a nonnegative integer from base 10 "to" base B. Routine F converts "from" base B (displayed in HEX mode) back to base 10.

E.g. "12 STO B 2013 XEQ T" displays "11B9" (2013 in base 12). Still in hex mode, type "1200 XEQ F", and it displays "2016".

Since the calculator only supports bases 2, 8, 10, and 16, arithmetic in other bases is rather awkward: "HEX 117A 11B9 XEQ F x<>y XEQ F - XEQ T" should display "3B" as the result of a base-12 subtraction.

Routine M could be inlined here, but it's useful by itself. Without upsetting the stack, it replaces y and x with the integer quotient and remainder of y/x. The INT÷ and RMDR operations give only one of these outputs, and they were introduced with the 33s model.

T01 LBL T
T02 0
T03 STO A
T04 R↓
T05 1
T06 STO C
T07 R↓
T08 SF 0
T09 XEQ B
T10 RCL A
T11 HEX
T12 RTN

F01 LBL F
F02 0
F03 STO A
F04 R↓
F05 1
F06 STO C
F07 R↓
F08 CF 0
F09 XEQ B
F10 RCL A
F11 DEC
F12 RTN

B01 LBL B
B02 16
B03 RCL B
B04 FS? 0
B05 x<>y
B06 R↓
B07 XEQ M
B08 RCL× C
B09 STO+ A
B10 R↑
B11 STO× C
B12 R↓
B13 R↓
B14 x>0?
B15 GTO B
B16 R↓
B17 RTN

M01 LBL M
M02 x<>y
M03 STO M
M04 x<>y
M05 ÷
M06 LASTx
M07 x<>y
M08 IP
M09 ×
M10 LASTx
M11 x<>y
M12 RCL- M
M13 +/-
M14 RTN

The HP 50g uses a more complex programming language called "User RPL". It's still essentially a series of keystrokes applied to an RPN stack, but with local variables and no line numbers or "goto" statements. The above programs look like

Program "tbase":
« "" → str «
  DO base IDIV2 DUP 9 > 7 * + 48 + CHR str + 'str' STO
  UNTIL DUP 0 ≤                       
  END
  DROP str
» »

Program "fbase":
« 0 → num «
  WHILE DUP SIZE 0 > 
  REPEAT base 'num' STO* DUP NUM R→I DUP 64 > 7 * - 48 - 'num' STO+ TAIL
  END
  DROP num
» »
"base" must be a global variable containing an integer between 2 and 36. This calculator supports enormous integers, so you can e.g. verify that 263-1 is 9223372036854775807 in decimal and "1Y2P0IJ32E8E7" in base 36.

The older HP-42S supports rudimentary string handling, so we can do the same base-36 calculation there. It works for integers up to 1012, or 1034 on the Free42 emulator.

01 LBL "TBASE"
02 CLA		; clear the only string register
03 LBL 00	; two-digit local label
04 ENTER
05 ENTER
06 RCL "BASE"	; This generates the least
07 MOD		; significant digit first
08 2
09 -
10 7		; close the seven-char gap between
11 X≥Y?		; "9" and "A" in the ASCII table
12 CLX
13 +
14 50
15 +
16 XTOA		; append ASCII digit [0-9A-Z]
17 R↓
18 -1		; rotate it to the front
19 AROT
20 R↓
21 RCL÷ "BASE"
22 IP
23 X>0?
24 GTO 00
25 R↓
26 AVIEW	; display the string register
27 END

01 LBL "FBASE"
02 0		; answer accumulates here
03 0		; garbage
04 LBL 00	; conditional jump to here
05 R↓		; leaves garbage on the stack
06 RCL× "BASE"
07 ATOX		; pull off the first char
08 50		; and de-ASCII it
09 -
10 7
11 X≥Y?
12 CLX
13 -
14 2
15 +
16 +		; add digit to answer
17 ALENG
18 X>0?
19 GTO 00
20 R↓
21 END