Post Reply 
How to change bases?
04-07-2019, 02:45 PM
Post: #1
How to change bases?
Say I have a number in base 3 and want to convert it to base 6 or any base conversion. How would I do this?
Find all posts by this user
Quote this message in a reply
04-07-2019, 03:31 PM
Post: #2
RE: How to change bases?
(04-07-2019 02:45 PM)kevin3g Wrote:  Say I have a number in base 3 and want to convert it to base 6 or any base conversion. How would I do this?

Prime natively supports only 4 bases; from page 696 in the manual:

Quote:...the HP Prime enables you to carry out integer arithmetic in four bases: decimal (base 10), binary, (base 2), octal (base 8), and hexadecimal (base 16). For example, you could multiply 4 in base 16 by 71 in base 8 and the answer is E4 in base 16. This is equivalent in base 10 to multiplying 4 by 57 to get 228.

But you could also write a program to do so...

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
04-07-2019, 03:50 PM
Post: #3
RE: How to change bases?
https://www.hpcalc.org/details/7719 Wink
Other similar apps on HPCalc.org.
Best,

Aries Smile
Find all posts by this user
Quote this message in a reply
04-08-2019, 12:44 AM
Post: #4
RE: How to change bases?
(04-07-2019 02:45 PM)kevin3g Wrote:  Say I have a number in base 3 and want to convert it to base 6 or any base conversion. How would I do this?
There may be a better way, but whenever I have written a base conversion program to convert from one base to another, if neither base is 10, I first convert the number to base 10, then to the other base.

To convert base 3 number to base 10, multiply digits by corresponding powers of 3 and add. To convert base 10 number to base 6, for example, divide the base 10 number by 6 and save the remainder, then divide the quotient by 6 and save the remainder, and repeat until quotient is 0. When you "save the remainders," first one is the rightmost digit of your answer.
Find all posts by this user
Quote this message in a reply
04-08-2019, 01:14 AM
Post: #5
RE: How to change bases?
Is this undocumented? Prime can convert any base-10 integer to any other base, and any number in any base to base 10. The only caveats are that you have to do it in CAS (not Home), you have to type the CONVERT command in lowercase letters, and you have to remember that the non-decimal base numbers are represented as arrays of integers in backwards order.

Examples in CAS:

convert(12345,base,6) --> [3,5,0,3,3,1]
... this means that 12345 in base 10 = "133053" in base 6

convert([3,5,0,3,3,1],base,6) --> 12345
... this means that "133053" in base 6 = 12345 in base 10
If you wish, you may use a list instead of an array here; both seem to work fine.

Since there is no upper limit for the base you specify, Prime does not use letters or other characters to stand for digit values above 9. It just gives you their decimal value, like this:

convert(1234,base,16) --> [2,13,4]
... this means that 1234 (base 10) = "4D2" in hex, with the "D" being shown as 13.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
04-08-2019, 01:36 AM
Post: #6
RE: How to change bases?
(04-08-2019 01:14 AM)Joe Horn Wrote:  Is this undocumented? Prime can convert any base-10 integer to any other base, and any number in any base to base 10. The only caveats are that you have to do it in CAS (not Home), you have to type the CONVERT command in lowercase letters, and you have to remember that the non-decimal base numbers are represented as arrays of integers in backwards order.

Examples in CAS:

convert(12345,base,6) --> [3,5,0,3,3,1]
... this means that 12345 in base 10 = "133053" in base 6

convert([3,5,0,3,3,1],base,6) --> 12345
... this means that "133053" in base 6 = 12345 in base 10
If you wish, you may use a list instead of an array here; both seem to work fine.

Since there is no upper limit for the base you specify, Prime does not use letters or other characters to stand for digit values above 9. It just gives you their decimal value, like this:

convert(1234,base,16) --> [2,13,4]
... this means that 1234 (base 10) = "4D2" in hex, with the "D" being shown as 13.

For me, Joe's comments, while undoubtedly correct, confirms my initial post, that Prime does not support other bases. Dodgy

Arrays of integers in backward order? Really? It's no wonder this isn't documented...

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
04-08-2019, 02:12 AM
Post: #7
RE: How to change bases?
(04-08-2019 01:36 AM)rprosperi Wrote:  Arrays of integers in backward order? Really? It's no wonder this isn't documented...

I like to pretend that Bernard did it that way on purpose, to make HP-71 programmers (for whom EVERYTHING is stored in backwards order, even memory addresses) feel right at home. Big Grin It's not true, but it's a pleasant fantasy.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
04-08-2019, 09:29 AM
Post: #8
RE: How to change bases?
I made the choice to be compatible with Maple.
Find all posts by this user
Quote this message in a reply
04-08-2019, 01:11 PM
Post: #9
RE: How to change bases?
(04-08-2019 01:36 AM)rprosperi Wrote:  For me, Joe's comments, while undoubtedly correct, confirms my initial post, that Prime does not support other bases. Dodgy

Arrays of integers in backward order? Really? It's no wonder this isn't documented...

It would be trivial to write a program to convert the backwards list to a string with A-Z and a-z representing base x digits similar to Infinite Base Calc.
Find all posts by this user
Quote this message in a reply
04-08-2019, 07:39 PM
Post: #10
RE: How to change bases?
(04-08-2019 01:11 PM)John Keith Wrote:  It would be trivial to write a program to convert the backwards list to a string with A-Z and a-z representing base x digits similar to Infinite Base Calc.

As clearly could (IMHO should) have been done either directly in CAS, or ideally also available for use in Home (where other number base options also exist). As it is, all 6 people that could recall how use convert() (it is not in the Help system or manual) are probably all set, a few others may run across some info in other xCAS manuals found online if they are hardy searchers, Steve Tuc (4 built-in bases only) and John Keith can roll their own, but most of the rest of us just shake our heads, wondering... Huh

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
04-10-2019, 12:46 AM (This post was last modified: 04-10-2019 12:47 AM by toml_12953.)
Post: #11
RE: How to change bases?
(04-08-2019 12:44 AM)Don Shepherd Wrote:  
(04-07-2019 02:45 PM)kevin3g Wrote:  Say I have a number in base 3 and want to convert it to base 6 or any base conversion. How would I do this?
There may be a better way, but whenever I have written a base conversion program to convert from one base to another, if neither base is 10, I first convert the number to base 10, then to the other base.

To convert base 3 number to base 10, multiply digits by corresponding powers of 3 and add. To convert base 10 number to base 6, for example, divide the base 10 number by 6 and save the remainder, then divide the quotient by 6 and save the remainder, and repeat until quotient is 0. When you "save the remainders," first one is the rightmost digit of your answer.

To convert 45 base 8 to base 16 you could nest converts like this:

convert(convert([5 4],base,8),base,16)

which would put out [5 2] since 25 is the hex equivalent of 45 octal. I know the Prime has these bases built-in but the power of this method is that you can use any bases, not just 8 and 16.

Tom L

French phrases you rarely hear: "trop de beurre" (too much butter)
Find all posts by this user
Quote this message in a reply
04-27-2019, 06:52 PM (This post was last modified: 04-27-2019 07:14 PM by Albert Chan.)
Post: #12
RE: How to change bases?
(04-10-2019 12:46 AM)toml_12953 Wrote:  To convert 45 base 8 to base 16 you could nest converts like this:

convert(convert([5 4],base,8),base,16)

which would put out [5 2] since 25 is the hex equivalent of 45 octal

I can not get used to digits in reversed order, so I flip it:

btb(lst, b1, b2) := reverse(convert(polyEval(lst, b1), base, b2))

btb([4,5], 8, 16) → [2,5], or 0x25

Example, decimal 123 to base 2

btb([1,2,3], 10, 2) or btb([123], 10, 2] → [1,1,1,1,0,1,1], or 0b1111011
Find all posts by this user
Quote this message in a reply
Post Reply 




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