Thursday, February 2, 2017

HP Prime and TI-84 Plus CE: Convert Integers: Decimal to Any Base

HP Prime and TI-84 Plus CE:  Convert Integers: Decimal to Any Base

Below are two programs that allow conversion of integers from decimal to any designated base.  Integers used in base other than 10 are presented in list format.  Each digit is separate and listed in order of descending place value.

For example: To input 1282_9 (base 9), enter {1, 2, 8, 2} and designate 9 as the base.

Digits are shown in numerical form and not in alphabetic.  Hence, if you were working for hexadecimal, enter 10 for A, 11 for B, and so on.

HP Prime Program DEC2BASE

EXPORT DEC2BASE(N,B)
BEGIN
// decimal, base
// 2017-01-31 EWS
LOCAL S,L1,Q,D,X;
S:=IP(LN(N)/LN(B))+1;
L1:=MAKELIST(0,X,1,S);
D:=N;
FOR K FROM 1 TO S DO
Q:=IP(D/B^(S-K));
L1(K):=Q;
D:=D-Q*B^(S-K);
END;
RETURN L1;

END;

See the TI-84 Plus CE section for examples.

HP Prime Program BASE2DEC

EXPORT BASE2DEC(L1,B)
BEGIN
// list of digits, base
// 2017-01-31 EWS
LOCAL S,N,K,Q;
S:=SIZE(L1);
N:=0;
FOR K FROM 1 TO S DO
N:=N+L1(K)*B^(S-K);
END;
RETURN N;
END;

See the TI-84 Plus CE section for examples.

TI-84 Plus CE Program DEC2BASE
(This should be work with the classic TI-84 Plus)

Input "DEC :",N
Input "TO BASE: ",B
int(ln(N)/ln(B))+1→S
S→dim(L1)
N→D
For(K,1,S)
int(D/B^(S-K))→Q
Q→ L1 (K)
D-Q*B^(S-K)→D
End
Disp ">BASE "+eval(B)
Pause L1

** For classic TI-84 or any operating system not working with OS 5.2 or later, you can leave out the “+eval(B)” for the second to the last line.

Examples:

Convert 1254 to base 6.
Result:  {5, 4, 5, 0}   (5450_6).
(5 * 6^3 + 4 * 6^2 + 5*6)

Convert 17291 to base 13.
Result:  {7, 11, 4, 1}   (7B41_13
(7 * 13^3 + 11 * 13^2 + 4 * 13 + 1)

(Here I use A = 10, B = 11, C = 12, and so on, like calculator notation)

TI-84 Plus CE Program BASE2DEC
(This should be work with the classic TI-84 Plus)

List:  {q_n, q_n-1, … , q_1, q_0} where
q_n * b^(n)
q_n-1 * b^(n-1)
q_1 * b
q_0  (constant)

Treat this like if you were entering a polynomial of coefficients of descending order from power n to 0.

Disp "LIST OF DIGITS"
Input L1
Input "BASE: ",B
dim(L1)→S
0→N
For(K,1,S)
N+ L1 (K)*B^(S-K)→N
End
Disp ">DEC"
Pause N

Examples:

Convert 4281_9 to decimal (4 * 8^3 + 2 * 9^2 + 8 * 9 + 1)
Input:  {4, 8, 2, 1}, Base: 9
Result: 3583

Convert C107_16 to decimal (12 * 16^3 + 1 * 16^2 + 7)
Input:  {12, 1, 0, 7}, Base: 16
Result:  49415

I hope you find this helpful.

Coming up, I am reading about the golden ratio, I will have another retro calculator review (Texas Instruments BA II from 1984) coming shortly, and I may have a guest post as well.  (10 posts away from post #700!)

Thank you everyone.  Welcome to February!

Eddie

This blog is property of Edward Shore, 2017