IGNORED

# Convert HEX directly to DEC?

## Recommended Posts

With the "second generation" BASICs (BXL, BXE, TBXL, Altirra Basic, etc.), is it possible to directly convert a HEX number into DEC? All of these BASICs have a HEX\$ function, and I thought there was a way to use the VAL function to do the reverse conversion. But there doesn't seem to be. (I know there is a short routine that can be used, but as I said, is there a direct way?)

-Larry

##### Share on other sites

?\$ABCD maybe

EDIT:

and if you mean to convert a hex number to a value (not just to a dec string), in TBXL there is DEC(), for example A=DEC("ABCD"). In BXE the VAL function you mention should work similarly, it just requires "\$" in front of the digits.

Edited by drac030
##### Share on other sites

Nevermind, I missed what you're asking. You want to convert in BASICs that already support HEX.

##### Share on other sites

?\$ABCD maybe

Yes! That works fine! So for example, DNUM=\$A9:? DNUM results in 169 being printed.

Thanks -- I'll make a note in my BXL manual!

-Larry

##### Share on other sites

With the "second generation" BASICs (BXL, BXE, TBXL, Altirra Basic, etc.), is it possible to directly convert a HEX number into DEC? All of these BASICs have a HEX\$ function, and I thought there was a way to use the VAL function to do the reverse conversion.

I use BASIC XL. Internally, every number is stored as a floating point decimal number, so there really is no "hexadecimal" number.

.

```A=\$FEDC

?A
65244

?\$BA98
47768

```

.

So, there is no need to convert a hexadecimal numeric constant since it is automatically converted to decimal when it is used or assigned to a variable.

Turning a string representing a hex value into decimal works (in BASIC XL) with VAL(), but it requires a leading dollar sign.

.

```?VAL("\$01FF")
511

##### Share on other sites

in TBXL there is DEC(), for example A=DEC("ABCD").

Be aware that this only functions correctly if your character string data is uppercase. I found this out when attempting to convert a lot of text files that were mostly hex data, where some of the files used all lower-case.

##### Share on other sites

Yes, that is a known TBXL bug, thanks for reminding about it.

##### Share on other sites

Thanks for the replies! This all started when I wanted a quick way to convert some hex numbers from a screen print. But as usual, I got the info I needed and learned more from our members as a bonus!

P.S. while looking for an quick solution, I found this little Atari BASIC program from Analog. This was the best one I found in my archives, but it doesn't include a decimal to hex routine. This was from Analog #17 from the BASIC Training column. It is set up to loop for repetitive conversion values.

-Larry

HexDec.atr

##### Share on other sites

BASIC Turbocharger has routines for both DEC -> HEX and HEX -> DEC. That's where I got the routine I'm using from.

[Edit: I should add, that the HEX -> DEC routine here will work with upper-case or lower-case input.]

Atarimania - BASIC Turbocharger

Edited by MrFish

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.