Saturday, February 4, 2017

HP Prime: Base Conversions – Joe Horn

HP Prime:  Base Conversions – Joe Horn

Special thanks to Joe Horn.  When I posted the conversion programs ( http://edspi31415.blogspot.com/2017/02/hp-prime-and-ti-84-plus-ce-convert.html ), Horn alerted me to a hidden convert command where we can convert base integers.

The HP Prime has a CONVERT (upper case letters) that converts units (i.e. length, volume, etc.).  However, for the base to work, not only convert has to be typed in lower case letters, AND it is a CAS command.

The syntax that Joe told me is:

Decimal to Other Base (D2B)

convert(number in base 10, base, desired base)

The word base is typed in lowercase.  The result is a vector of digits but the order goes from lower power to upper power.  The number will be written backwards.  To get it in proper order:

REVERSE(convert(number in base 10, base, desired base))

In Home mode or in programming, you will need a CAS command attached to the syntax.

Other Base to Decimal

convert(vector of digits, base, base that is represented)

Again, going this way the vector of digits goes from lower to upper power.  The proper order requires the following:

convert(REVERSE(vector of digits), base, base that is represented)

Once again, in Home mode or in programming, you will need a CAS command attached to the syntax.

Below are programs that help accomplish these tasks:

HP Prime Program D2B (decimal to base)

EXPORT D2B(n,b)
BEGIN
// decimal to base
// Joe Horn
CAS("REVERSE(convert(n,base,b))");
END;

Example:  D2B(17291, 12) returns [10 0 0 11]   (A00B_12)

HP Prime Program B2D (base to decimal)

EXPORT B2D(n,b)
BEGIN
// vector (decr),base to base 10
// Joe Horn
CAS("convert(REVERSE(n),base,b)");

END;

Example:  B2D([1, 0, 1, 6, 2, 5], 7) returns 17463.  (101625_7)

Thank you Joe!  As Joe Horn always says, Happy Programming!

Eddie

This blog is property of Edward Shore, 2017.



3 comments:

  1. See the profiles of experts named Eddie Choufi on twitter. There are 3 experts named Eddie Choufi, who utilize twitter to trade data. See what Eddie choufi (eddiechoufi) has found on twitter, the world's greatest gathering of the loved and admired things.

    ReplyDelete
  2. Hi, I added a bit to render the output more readable aka useful.
    See below :

    EXPORT Dec2Base(n,b)
    BEGIN
    LOCAL r := iPart(ROUND(n,0));
    RETURN CAS("char(apply((x)->PIECEWISE(10>x,x+48,x≥10,x+55),REVERSE(convert(r,base,b))))");
    END;

    EXPORT Base2Dec(n,b)
    BEGIN
    RETURN CAS("convert(apply((x)->PIECEWISE(58>x,x-48,x≥58,x-55),REVERSE(ASC(n))),base,b)");
    END;

    I guess, it could be improved further. Cheers

    ReplyDelete

HHC 2017 In Review

HHC 2017 In Review Hello Nashville, TN! HHC 2017 took place on September 16 and 17, 2017 in Brentwood, TN.  If you have not go...