Base Conversion
05-12-2016, 06:46 AM (This post was last modified: 05-12-2016 09:14 AM by Stevetuc.)
Post: #1
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
Base Conversion
This program will convert between any base, with the user being prompted for the required output base.
It uses B→R() to first convert any base to decimal, then R→B() to convert to desired base:
Code:
EXPORT Baseconv(a) BEGIN LOCAL b; CHOOSE(b, "Base", "System", "Binary","Octal","Decimal","Hex"); R→B(B→R(a),32,b-1) END;
I find it convenient to access via the Toolbox and User soft key.
I have hardcoded bits as 32. This could be set via another choose() or passed as Fn parameter.

If you prefer to access via a user key, it can be done like this:
Code:
KEY K_3() BEGIN "Baseconv()"; END;
I use key 3 because the shift # acts as mnemonic for Base.
Either method will work with rpn mode , so long as the input is already on the stack.

EDIT: replace 32 in R→B(B→R(a),32,b-1) with GETBITS(#) to use the current system setting for integer size

Steve
05-12-2016, 09:21 AM
Post: #2
 jrozsas Member Posts: 158 Joined: Nov 2014
RE: Base Conversion
Very good. It works perfectly. But is there any way to convert decimal to binary fraction with? Example: 10.5 (dec) to binary

Leo

05-12-2016, 09:55 AM
Post: #3
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
a possible variant to choose "hardcoding"...

Code:
 EXPORT Baseconv(a) BEGIN LOCAL b, hardb; CHOOSE(b, "Base", "System", "Binary","Octal","Decimal","Hex"); CHOOSE(hardb,"Hardcoding","8","16","32","64"); // R→B(B→R(a),32,b-1); R→B(B→R(a),EVAL(2^(hardb+2)),b-1) END;

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
05-12-2016, 01:10 PM
Post: #4
 jrozsas Member Posts: 158 Joined: Nov 2014
RE: Base Conversion
Binary 1010 to decimal = #1010d ?

Leo

05-12-2016, 01:13 PM
Post: #5
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
(05-12-2016 01:10 PM)jrozsas Wrote:  Binary 1010 to decimal = #1010d ?

hi Leo,
Baseconv(#1010b) return #10d

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
05-12-2016, 02:11 PM (This post was last modified: 05-12-2016 02:12 PM by Stevetuc.)
Post: #6
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversion
Updated to allow bitwidth to be chosen as Salvomic proposed, and also allow default system bitwidth option.
More meaningful variable names than a,b have used for readability.

Code:
EXPORT Baseconv(in) BEGIN LOCAL base,bits; CHOOSE(base, "Base", "System", "Binary","Octal","Decimal","Hex"); CHOOSE(bits, "Size", "System","8", "16","32","64"); IF bits > 1 THEN bits:=2^(bits+1) ELSE bits:=GETBITS(#);END; R→B(B→R(in),bits,base-1) END;

Steve
05-12-2016, 02:27 PM
Post: #7
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
thank you Steve!
it works fine!

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
05-12-2016, 04:42 PM
Post: #8
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversion
Small tweak to allow option to use the input bitwidth:

Code:
EXPORT Baseconv(in) BEGIN LOCAL base,bits; CHOOSE(base, "Base", "System", "Binary","Octal","Decimal","Hex"); CHOOSE(bits, "Size","Input","System","8", "16","32","64"); CASE IF bits=1 THEN bits:=GETBITS(in) END; IF bits=2 THEN bits:=GETBITS(#) END; DEFAULT bits:=2^(bits) END; R→B(B→R(in),bits,base-1) END;

Steve
05-12-2016, 04:52 PM (This post was last modified: 05-12-2016 05:42 PM by salvomic.)
Post: #9
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
if the user choose Input the program return
Error: Invalid input
with Baseconv(255)

It works well with Baseconv(#255d)

The error is given when the program uses
GETBITS(255)
it should have an input like GETBITS(#255d)
so it's needed a control when the user input a normal decimal number without # and the final "d"...

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
05-12-2016, 06:49 PM
Post: #10
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversion
(05-12-2016 04:52 PM)salvomic Wrote:  please, check the code:
if the user choose Input the program return
Error: Invalid input
with Baseconv(255)

It works well with Baseconv(#255d)

Salvo, This works with both Baseconv(255) and Baseconv(#255d)
Hopefully no introduced bugs!
[/code]
Code:
EXPORT Baseconv(in) BEGIN LOCAL base,bits; CHOOSE(base, "Base", "System", "Binary","Octal","Decimal","Hex"); CHOOSE(bits, "Size","Input","System","8", "16","32","64"); IF LEFT(STRING(in),1) ≠ "#" THEN in:=EXPR("#"+in) END; CASE IF bits=1 THEN bits:=GETBITS(in) END IF bits=2 THEN bits:=GETBITS(#) END DEFAULT bits:=2^(bits) END; R→B(B→R(in),bits,base-1) END;
05-12-2016, 07:12 PM (This post was last modified: 05-12-2016 07:21 PM by salvomic.)
Post: #11
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
(05-12-2016 06:49 PM)Stevetuc Wrote:  Salvo, This works with both Baseconv(255) and Baseconv(#255d)
Hopefully no introduced bugs!

hi Stevetuc,
it's ok to overcome the error but there is a last thing to solve:
Baseconv(255) is seen always as it was Baseconv(#255h), as hex, so
Baseconv(255) -> Hex -> Input return #255h and not the correct #FFh, I think that user mean 255 *is* decimal, otherwise it would have write #255h (597d) :-)
With this code
Code:
IF LEFT(STRING(in),1) ≠ "#" THEN in:=EXPR("#"+in+"d") END;
it works interpreting 255 as decimal.
But... But if the user inputs
Baseconv(#255) still the code interprets as #255h (it adds the "h" for hex) :-)
In this case, maybe the user really means to say "#255h", otherwise he/she doesn't put the "#" first...

However, besides that, it's ok!
thank you
Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
05-12-2016, 07:26 PM (This post was last modified: 06-08-2016 06:47 AM by Stevetuc.)
Post: #12
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversion
(05-12-2016 07:12 PM)salvomic Wrote:
(05-12-2016 06:49 PM)Stevetuc Wrote:  Salvo, This works with both Baseconv(255) and Baseconv(#255d)
Hopefully no introduced bugs!

hi Stevetuc,
it's ok to overcome the error but there is a last thing to solve:
Baseconv(255) is seen always as it was Baseconv(#255h), as hex, so
Baseconv(255) -> Hex -> Input return #255h and not the correct #FFh, I think that user mean 255 *is* decimal, otherwise it would have write #255h (597d) :-)
With this code
Code:
IF LEFT(STRING(in),1) ≠ "#" THEN in:=EXPR("#"+in+"d") END;
it works interpreting 255 as decimal.
But... But if the user inputs
Baseconv(#255) still the code interprets as #255h (it adds the "h" for hex) :-)
In this case, maybe the user really means to say "#255h", otherwise he/she doesn't put the "#" first...

However, besides that, it's ok!
thank you
Salvo

I think what happens is that it assumes the number is in whatever the default base is set to in settings. Mine is in decimal so it treats unqualified numbers as such. Hard coding to decimal will allow to override the settings but I guess this may not always produce the desired result

Thanks for helping me debug this!
Steve
05-12-2016, 07:31 PM
Post: #13
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
(05-12-2016 07:26 PM)Stevetuc Wrote:  I think what happens is that it assumes the number is in whatever the default base is set to in settings. Mine is in decimal so it treats unqualified numbers as such. Hard doing to decimal will allow to override the settings but I guess this may not allows produce the desired result

Thanks for helping me debug this!
Steve

yes, I agree.
I think the program is already good enough for us!
Very useful, thank you.

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
06-07-2016, 01:08 PM
Post: #14
 Eddie W. Shore Senior Member Posts: 1,590 Joined: Dec 2013
RE: Base Conversion
Very nice program, gentlemen. I like the easier interface that the program presents.
06-09-2016, 03:22 PM (This post was last modified: 06-09-2016 03:28 PM by Stevetuc.)
Post: #15
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversion
Thanks Eddie.

That was my motivation for writing it, I wanted an easy to use function for base conversion.
I've updated the program with the following.

Numbers without # are treated as decimal - as suggested by Salvomic

Numbers with # but without explicit type are treated as per system setting for integers

Unsigned/Signed support - if width is selected as 8,16,32 or 64 a choose box for unsigned/signed is presented.
If Width is selected as System, then system setting for sign state is used.
If Width is selected as Input, then the input sign state is used.

Examples
( for this example system setting is hex 16bit unsigned)

Baseconv(#−9:-15d)
converted to binary 16 bit unsigned = #1111111111110111b

Baseconv(#−9:-15d)
Converted to binary 16bit signed= #−1001:-15b

Baseconv(#−9:-15d)
Converted to decimal 16bit unsigned = #65527d

In the first and last case, the size :16 is not explicitly shown because it matches the current system width. If the system width subsequently changes, then these will become explicit.

Code:
EXPORT Baseconv(in) BEGIN LOCAL base,bits,sign; CHOOSE(base, "Base", "System", "Binary","Octal","Decimal","Hex"); CHOOSE(bits, "Size","System","Input","8","16","32","64"); IF bits >2 THEN CHOOSE(sign, "Sign","Unsigned","Signed") END; IF LEFT(STRING(in),1) ≠ "#" THEN in:=EXPR("#"+in+"d") END; //correct the format CASE IF bits=1 THEN bits:=GETBITS(#) END //use system bitw IF bits=2 THEN bits:=GETBITS(in) END //use input bitw DEFAULT bits:=2^(bits) END; IF sign=2 THEN bits:=1-bits END; // SETBITS(SETBASE(in,base-1),bits) //alternative command R→B(B→R(in),bits,base-1) END;
06-09-2016, 03:33 PM
Post: #16
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
great improvement!
thank you.

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
06-10-2016, 09:19 AM (This post was last modified: 03-20-2017 06:57 AM by Stevetuc.)
Post: #17
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversion
One more tweak :-)

Using the alternative command:
SETBITS(SETBASE(in,base-1),bits)
Turns out to be superior since there is no need to detect numbers entered without # , it handles them by default as decimal.
Therefore we can remove this line:
IF LEFT(STRING(in),1) ≠ "#" THEN in:=EXPR("#"+in+"d") END

Code:
EXPORT Baseconv(in) BEGIN LOCAL base,bits,sign; CHOOSE(base, "Base", "System", "Binary","Octal","Decimal","Hex"); CHOOSE(bits, "Size","System","Input","8","16","32","64"); IF bits >2 THEN CHOOSE(sign, "Sign","Unsigned","Signed") END; //IF LEFT(STRING(in),1) ≠ "#" THEN in:=EXPR("#"+in+"d") END; //correct the format - no longer needed CASE IF bits=1 THEN bits:=GETBITS(#) END //use system bitw IF bits=2 THEN bits:=GETBITS(in) END //use input bitw DEFAULT bits:=2^(bits) END; IF sign=2 THEN bits:=1-bits END; SETBITS(SETBASE(in,base-1),bits);  //R→B(B→R(in),bits,base-1) //old command END;
06-10-2016, 09:51 AM
Post: #18
 salvomic Senior Member Posts: 1,396 Joined: Jan 2015
RE: Base Conversion
it works well, thank you!
Simple and useful program.

Salvo

∫aL√0mic (IT9CLU) :: HP Prime 50g 41CX 71b 42s 39s 35s 12C 15C - DM42, DM41X - WP34s Prime Soft. Lib
06-11-2016, 07:53 AM
Post: #19
 Stevetuc Senior Member Posts: 313 Joined: Jan 2014
RE: Base Conversion
(06-10-2016 09:51 AM)salvomic Wrote:  it works well, thank you!
Simple and useful program.

Salvo

Thanks Salvo!
08-16-2018, 03:56 AM
Post: #20
 JDW Senior Member Posts: 437 Joined: Jun 2016
RE: Base Conversion
(06-10-2016 09:19 AM)Stevetuc Wrote:  One more tweak :-)

Can somehow explain how to properly install and run these programs?

I copied the text of the program listed in the earlier post, then I pasted it into a new TextWrangeler document on my Mac and saved it a UTF-8 file. Here is the file. But when I drag that file from off my Desktop and onto my Prime in the Connectivity Kit, the Connectivity Kit crashes. I crashes every time I try. Is the filename extension bad? Is the encoding bad? What's the trick?

And once installed on my Prime (assuming I can ever get around these crashes), how do I execute the program?

Thanks.
 « Next Oldest | Next Newest »

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