Base Conversions (to any base 2-36)
12-28-2019, 06:27 PM
Post: #1
 Eddie W. Shore Senior Member Posts: 1,598 Joined: Dec 2013
Base Conversions (to any base 2-36)

Introduction

The program BASEDEC converts a number in a base representation to decimal (base 10). BASEDEC accepts any positive number, including non-integers up to base 36. Each of the letters of alphabet represents a value:

A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
...
Z = 35

BASEDEC takes two arguments: number to be converted, entered as a string; and the base representation of the number.

Examples:

BASEDEC("531",8) converts 531_8 to decimal. Result: 345

BASEDEC("63.95A", 11) converts 63.95A_11 to decimal. Result: 69.86701728

BASEDEC("4H", 18) converts 4H_18 to decimal. Result: 89 (H = 17)

HP Prime Program BASEDEC
Code:

EXPORT BASEDEC(s,b)
BEGIN
// string, original base
// number can have fractional
// part in decimal form
// convert to base 10
// A=10,B=11,C=12...Z=35
// 2019-12-09 EWS
LOCAL l,pt,sl,sr,ls,lr,d;
LOCAL k,ac,al,dg,sd;
d:=0; // decimal
l:=DIM(s);
// split the string
pt:=INSTRING(s,".");
CASE
IF pt==0 THEN
sl:=s;
sr:="";
END;
IF pt==1 THEN
sl:="";
sr:=RIGHT(s,l-1);
END;
DEFAULT
sl:=LEFT(s,pt-1);
sr:=RIGHT(s,l-pt);
END;
// conversion to decimal
// integer part
ls:=DIM(sl);
IF ls>0 THEN
FOR k FROM 1 TO ls DO
sd:=MID(sl,k,1);
ac:=ASC(sd);
al:=ac(1);
dg:=when(al>64,al-55,EXPR(sd));
d:=d+dg*b^(ls-k);
END;
END;
// fractional part
lr:=DIM(sr);
IF lr>0 THEN
FOR k FROM 1 TO lr DO
sd:=MID(sr,k,1);
ac:=ASC(sd);
al:=ac(1);
dg:=when(al>64,al-55,EXPR(sd));
d:=d+dg/(b^k);
END;
END;
// return decimal
RETURN d;
END;

Introduction Part II

The program DECBASE converts a number in decimal form to any base you want. DECBASE works will all positive real numbers. There are three arguments to DECBASE:

* The number in decimal form
* The desired base
* Accuracy: how many decimal points do you want. Conversions from decimal to other bases are approximations. If the number is a integer (frac(number) = 0), then this argument is ignored (put 0).

The result is returned as a string.

Examples:

DECBASE(69, 6, 0) converts 69 to base 6. Result: "153"

DECBASE(5.875, 8, 4) converts 5.875 to base 8 to four decimals. Result: "5.7000" (exact: 5.7_8)

DECBASE(635.05, 12, 12) converts 635.05 to base 12 to twelve decimals. Result: "44B.072497249724" (exact: 44B.07249..._12 (7249 repeats))

HP Prime Program DECBASE
Code:

EXPORT DECBASE(d,b,acc)
BEGIN
// decimal, base, accuracy
// convert from decimal to base
// 2019-12-09 EWS
LOCAL n,f,nl,nd;
LOCAL nf,k,dg,s,fd;
n:=IP(d); // integer
f:=FP(d); // fractional
s:="";
// integer
IF n>0 THEN
nl:=IP(LOG(n)/LOG(b));
nd:=n;
FOR k FROM nl DOWNTO 0  DO
dg:=IP(nd/(b^k));
IF dg<10 THEN
s:=s+STRING(dg);
ELSE
s:=s+CHAR(dg+55);
END;
nd:=nd-dg*b^k;
END;
END;
// numbers < 1
IF n==0 THEN
s:="0";
END;
// fractional part
IF f>0 THEN
s:=s+".";
fd:=f;
FOR k FROM 1 TO acc DO
dg:=IP(fd*b^k);
IF dg<10 THEN
s:=s+STRING(dg);
ELSE
s:=s+CHAR(dg+55);
END;
fd:=fd-dg/(b^k);
END;
END;
RETURN s;
END;
12-29-2019, 04:48 PM
Post: #2
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversions (to any base 2-36)
Nice programs!
if I run the program using Shift Program and use the fill form all is well, but if I run from home screen as
BASEDEC("531",8) I get
Code:
0+when(al>64,al-55,EXPR(sd))*324+when(al>64,al-55,EXPR(sd))*18+when(al>64,al-55,EXPR(sd))*1

This is on android 2.1.14346 (2019 11 21)
Wonder why the when() statements not being evaluated?
12-30-2019, 04:26 PM
Post: #3
 Eddie W. Shore Senior Member Posts: 1,598 Joined: Dec 2013
RE: Base Conversions (to any base 2-36)
That's a strange result. Possibly a bug?

Thank you for the compliment!
01-01-2020, 05:42 AM
Post: #4
 Joe Horn Senior Member Posts: 2,005 Joined: Dec 2013
RE: Base Conversions (to any base 2-36)
Happy 2PI! (in base 26)

<0|ΙΈ|0>
-Joe-
 « Next Oldest | Next Newest »

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