Base Conversion
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;
 « Next Oldest | Next Newest »

 Messages In This Thread Base Conversion - Stevetuc - 05-12-2016, 06:46 AM RE: Base Conversion - jrozsas - 05-12-2016, 09:21 AM RE: Base Conversion - salvomic - 05-12-2016, 09:55 AM RE: Base Conversion - jrozsas - 05-12-2016, 01:10 PM RE: Base Conversion - salvomic - 05-12-2016, 01:13 PM RE: Base Conversion - Stevetuc - 05-12-2016, 02:11 PM RE: Base Conversion - salvomic - 05-12-2016, 02:27 PM RE: Base Conversion - Stevetuc - 05-12-2016, 04:42 PM RE: Base Conversion - salvomic - 05-12-2016, 04:52 PM RE: Base Conversion - Stevetuc - 05-12-2016, 06:49 PM RE: Base Conversion - salvomic - 05-12-2016, 07:12 PM RE: Base Conversion - Stevetuc - 05-12-2016, 07:26 PM RE: Base Conversion - salvomic - 05-12-2016, 07:31 PM RE: Base Conversion - Eddie W. Shore - 06-07-2016, 01:08 PM RE: Base Conversion - Stevetuc - 06-09-2016 03:22 PM RE: Base Conversion - salvomic - 06-09-2016, 03:33 PM RE: Base Conversion - Stevetuc - 06-10-2016, 09:19 AM RE: Base Conversion - JDW - 08-16-2018, 03:56 AM RE: Base Conversion - Tyann - 08-16-2018, 05:36 AM RE: Base Conversion - JDW - 08-16-2018, 10:02 AM RE: Base Conversion - Stevetuc - 08-16-2018, 11:21 AM RE: Base Conversion - JDW - 08-16-2018, 11:43 AM RE: Base Conversion - Stevetuc - 08-16-2018, 12:19 PM RE: Base Conversion - JDW - 08-17-2018, 01:46 AM RE: Base Conversion - Stevetuc - 08-17-2018, 04:13 AM RE: Base Conversion - salvomic - 06-10-2016, 09:51 AM RE: Base Conversion - Stevetuc - 06-11-2016, 07:53 AM

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