Saturday, March 16, 2013

Decimal/Binary Conversions for the HP 39gii



Decimal and Binary Conversions
Programs created 2/20/2013

Decimal to Binary (number → string)

D2B(number) - execute from Home!


EXPORT D2B(N)
BEGIN
LOCAL str1;
D:=INT(N);
L:=INT(LN(D)/LN(2));
str1:="1";
D:=D-2^L;
FOR K FROM 1 TO L DO
IF 2^(L-K)≤D THEN
str1:=str1+"1";
D:=D-2^(L-K);
ELSE
str1:=str1+"0";
END;
END;
RETURN str1;
END;


Examples:

D2B(52) returns "110100"

D2B(131) returns "10000011"

Binary to Decimal (string → number)

Run this from Home! B2D(string of 1s and 0s). Attempting to run B2D from the program catalog will cause an error.

EXPORT B2D(str1)
BEGIN
D:=dim(str1);
N:=0;
FOR K FROM 0 TO D-1 DO
N:=N+2^K*expr(mid(str1,D-K,1));
END;
RETURN N;
END;


Examples:

B2D("11011") returns 27.

B2D("1010110000") returns 688.


Eddie



This blog is property of Edward Shore. 2013




  Casio fx-7000G vs Casio fx-CG 50: A Comparison of Generating Statistical Graphs Today’s blog entry is a comparison of how a hist...