[Gambas-user] Val()

John Anderson johna at starflightinc.com
Thu May 27 15:31:59 CEST 2021


On 5/26/2021 11:39 PM, CD wrote:
> Hello,
>
> why Val("&FFFF") = -1
> and
> why Val("&FFFFFFFF") = -1
>
> is it normal?
>
> attached a TestVal project
> Regards
>
>
>
Yes, that is correct: Val() returns a numeric value evaluated to 
shortest integer type.  You have specified Short (2 bytes) in first 
case, and a Long (4 bytes) in the second case.  In both cases the 
highest order bit of the variable is 1, hence both cases evaluate to a 
value -1.

You can also do Val("&0000FFFF")  = 65535 ' The string dictates a Long

Or Val("&0FFFF")  = 65535  ' This string is considered a long also

Or Val("&HFFFF")  = -1  ' This string as a more formal designation for 
Hex (Like VB)

Or Val("&x1111111111111111")  = -1   ' Binary string tells Gambas this 
is a 2-Byte Short (similar to VB behaviour)

Or Val("&x11111111")  = 255   ' Binary string tells Gambas this is a 
Byte (Like VB) - unsigned value - I haven't tested this extensively, 
could also be considering this as Short

The documentation here is incorrect I think - it has left out the part 
about "Short" (and maybe Byte) types being returned if possible - I 
think that's where the confusion started:

https://gambaswiki.org/wiki/lang/val

-John


More information about the User mailing list