# HP Forums

Full Version: Base Conversion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
Very good. It works perfectly. But is there any way to convert decimal to binary fraction with? Example: 10.5 (dec) to binary
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;```
Binary 1010 to decimal = #1010d ?
(05-12-2016 01:10 PM)jrozsas Wrote: [ -> ]Binary 1010 to decimal = #1010d ?

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

Salvo
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
thank you Steve!
it works fine!

Salvo
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
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"...
(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 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
(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: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
Very nice program, gentlemen. I like the easier interface that the program presents.
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;```
great improvement!
thank you.

Salvo
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;```
it works well, thank you!
Simple and useful program.

Salvo
(06-10-2016 09:51 AM)salvomic Wrote: [ -> ]it works well, thank you!
Simple and useful program.

Salvo

Thanks Salvo!