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.



Sum of Sequential Integers (featuring Swiss Micros DM32)

  Sum of Sequential Integers ( featuring Swiss Micros DM32) What is the sum of the following: 100 + 101 + 102 + 103 + 104 + 105 + … + ...