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
Cui bono?
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
11-29-2019, 06:34 PM
Post: #13
RE: How to change bases?
I just stumbled upon a way to force Prime to return base-converted numbers in correct digit order (not backwards digit order as seen above in this thread).

Just put "base" in quotes. That's all it takes! And it even works in Home and in ordinary (non-CAS) programs!

Example in Home:
CONVERT(1234567, "base", 12) --> [ 4 11 6 5 4 7 ]
... this means that 1234567 in decimal equals 4B6547 in base 12.

The reverse can also be done like this:
CONVERT( [4,11,6,5,4,7], "base", 12) --> 1234567
... but I prefer this simpler method:
POLYEVAL( [4,11,6,5,4,7], 12) --> 1234567

Note: This also works in CAS and in CAS programs, but then you have to be careful to type CONVERT in uppercase letters. The "base" option is always case insensitive, and must be in quote marks to return the digits in correct order. Due to a bug, negative inputs for the first argument can crash, so for now only use positive inputs. Negative and non-integer bases return interesting results.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
11-30-2019, 04:48 AM
Post: #14
RE: How to change bases?
(11-29-2019 06:34 PM)Joe Horn Wrote:  I just stumbled upon a way to force Prime to return base-converted numbers in correct digit order (not backwards digit order as seen above in this thread).

Just put "base" in quotes. That's all it takes! And it even works in Home and in ordinary (non-CAS) programs!

Example in Home:
CONVERT(1234567, "base", 12) --> [ 4 11 6 5 4 7 ]
... this means that 1234567 in decimal equals 4B6547 in base 12.

The reverse can also be done like this:
CONVERT( [4,11,6,5,4,7], "base", 12) --> 1234567
... but I prefer this simpler method:
POLYEVAL( [4,11,6,5,4,7], 12) --> 1234567

Note: This also works in CAS and in CAS programs, but then you have to be careful to type CONVERT in uppercase letters. The "base" option is always case insensitive, and must be in quote marks to return the digits in correct order. Due to a bug, negative inputs for the first argument can crash, so for now only use positive inputs. Negative and non-integer bases return interesting results.

Great info, Joe! Is that documented anywhere but here?

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
11-30-2019, 01:01 PM
Post: #15
RE: How to change bases?
(11-30-2019 04:48 AM)toml_12953 Wrote:  Great info, Joe! Is that documented anywhere but here?

Not that I'm aware of.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
11-30-2019, 02:41 PM
Post: #16
RE: How to change bases?
(11-30-2019 01:01 PM)Joe Horn Wrote:  
(11-30-2019 04:48 AM)toml_12953 Wrote:  Great info, Joe! Is that documented anywhere but here?

Not that I'm aware of.

Since that syntax is undocumented and hardly intuitive, how on earth did you discover it? I usually find the stories behind such discoveries (think the 42S hex table at Taco Bell) at least as interesting as the actual result itself.

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
11-30-2019, 03:16 PM
Post: #17
RE: How to change bases?
(11-30-2019 02:41 PM)rprosperi Wrote:  
(11-30-2019 01:01 PM)Joe Horn Wrote:  Not that I'm aware of.

Since that syntax is undocumented and hardly intuitive, how on earth did you discover it? I usually find the stories behind such discoveries (think the 42S hex table at Taco Bell) at least as interesting as the actual result itself.

+1

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
11-30-2019, 06:26 PM
Post: #18
RE: How to change bases?
(11-30-2019 02:41 PM)rprosperi Wrote:  
(11-30-2019 01:01 PM)Joe Horn Wrote:  Not that I'm aware of.

Since that syntax is undocumented and hardly intuitive, how on earth did you discover it? I usually find the stories behind such discoveries (think the 42S hex table at Taco Bell) at least as interesting as the actual result itself.

Joe Horn is the Chuck Norris of the HP calculator world. I bet that function did not exist before Joe thought it upSmile
Visit this user's website Find all posts by this user
Quote this message in a reply
11-30-2019, 07:21 PM
Post: #19
RE: How to change bases?
(11-30-2019 02:41 PM)rprosperi Wrote:  Since that syntax is undocumented and hardly intuitive, how on earth did you discover it? I usually find the stories behind such discoveries (think the 42S hex table at Taco Bell) at least as interesting as the actual result itself.

Simplified answer: By playing with it.

Complete answer: By trying as many possible inputs and syntaxes and permutations thereof as I could think of... preferably ones which are NOT described in any documentation. That, of course, is exactly the method that we old-time PPC members used to discover the hidden features and bugs in the old HP models. That method still works, and is still fun, even when it doesn't discover anything, because it's a form of play.

<0|ɸ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
11-30-2019, 08:34 PM (This post was last modified: 11-30-2019 08:35 PM by toml_12953.)
Post: #20
RE: How to change bases?
(11-30-2019 07:21 PM)Joe Horn Wrote:  
(11-30-2019 02:41 PM)rprosperi Wrote:  Since that syntax is undocumented and hardly intuitive, how on earth did you discover it? I usually find the stories behind such discoveries (think the 42S hex table at Taco Bell) at least as interesting as the actual result itself.

Simplified answer: By playing with it.

Complete answer: By trying as many possible inputs and syntaxes and permutations thereof as I could think of... preferably ones which are NOT described in any documentation. That, of course, is exactly the method that we old-time PPC members used to discover the hidden features and bugs in the old HP models. That method still works, and is still fun, even when it doesn't discover anything, because it's a form of play.

I play with it every day and would have never come up with that!

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
Post Reply 




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