# Why will Val("&H") sometimes return a negative value?

by Solitaire (Login Solitaire1)
S

DIM stx AS STRING, num AS DOUBLE
CLS
INPUT "Enter a hex value: ", stx\$
num = VAL("&H" + stx)
PRINT num

For example, entering AB34 will return -21758 instead of 43828.

How can the code be changed to display the correct answer?

Posted on Jun 7, 2012, 6:49 PM

 Response Title Author and Date Re: Why will Val("&H") sometimes return a negative value? Michael Calkins on Jun 7 Thank you. More code added: Solitaire on Jun 8 Re: Thank you. More code added: Michael Calkins on Jun 8 dnum is a double because... Solitaire on Jun 9 Re: dnum is a double because... Michael Calkins on Jun 9 Val() is considered a legacy function. Solitaire on Jun 9 VAL has nothing to do with INTEGERS in QB either Clippy on Jun 9 VAL conversion Solitaire on Jun 10 *Val() legacy ? but useful lisztfr on Jun 10 LONG values, like INTEGERS use the top bit as the sign bit Clippy on Jun 7 All values greater than or equal to &H8000 (16-bit) or &H80000000& are negative numbers Bill Gates on Jun 8