Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacement for VAL with &H, &B, &O
#6
&H is HEX
&B is Binary
&O is Octal

So if you just say FFFF, that's a variable called FFFF -- but &HFFFF is the number -1.  (It's a signed integer and in hex, a signed integer of FFFF is -1.)

X = FFFF    << Assigning the variable FFFF's value to X.
X = &HFFFF&  <<  Assigning the long hex value of FFFF to X.  (65535, in this case.)

Same with Binary.

10 = ... well, 10.  It's a normal decimal number.
&B10 = 2, represented as a binary number.
&B101 = 5, and so on.

So VAL will work with your variables, but it doesn't work exactly the same as the &H, &B, &O variations do directly from the IDE.

PRINT &HFFFF     << -- this will print -1
PRINT VAL("&HFFFF")     <<-- this will print 65535.

Note that in hex, both -1 *and* 65535 are represented by FFFF -- depending on if you have a signed integer or an unsigned integer that you're casting the value to.  Both are *correct* answers, but they're not *consistent* -- which is what this little routine works to fix for us.  It doesn't mix signed and unsigned and keeps consistent results for us.


Think SQR(4).... Both 2 *and* -2 are proper answers to that SQR(4), though we usually only ever deal with the positive result.  In this case it would be like saying:

PRINT SQR(4)    <--- prints 2.
PRINT VAL("SQR(4)")    <--- prints -2.

Both are technically correct, but the results aren't consistent with each other and that can be a real PITA if you're comparing direct values vs string parsed values.
Reply


Messages In This Thread
Replacement for VAL with &H, &B, &O - by SMcNeill - 04-24-2025, 06:47 PM
RE: Replacement for VAL with &H, &B, &O - by Petr - 04-25-2025, 04:46 AM
RE: Replacement for VAL with &H, &B, &O - by Petr - 04-26-2025, 10:51 AM
RE: Replacement for VAL with &H, &B, &O - by SMcNeill - 04-26-2025, 01:59 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)