Saturday, December 28, 2019

HP Prime: Base Conversions

HP Prime:  Base Conversions 

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

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

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;


Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.