[Gambas-user] Val()
Benoît Minisini
g4mba5 at gmail.com
Thu May 27 17:41:08 CEST 2021
Le 27/05/2021 à 15:58, John Anderson a écrit :
> 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
>>
>>
>>
>
> <APOLOGIES - CORRECTIONS ADDED>
>
> Yes, that is correct: Val() returns a numeric value evaluated as the
> -shortest- integer type. You have specified Short (2 bytes) in first
> case, and a Long Integer (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
> Integer
>
> Or Val("&0FFFF") = 65535 ' This string is considered a Long Integer 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) type evaluations being returned if
> possible - I think that's where the confusion started:
>
> https://gambaswiki.org/wiki/lang/val
>
> -John
>
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
I don't see where the documentation of Val() is incorrect, it does not
talk about the difference between "&HFFFF" and "&HFFFF&", which is a VB
horror introduced in Gambas to help old VB users.
The rule is the following:
If the number starts with "&" or "&H" (hexdecimal), "&X" or "%"
(binary), and if the number -does not- ends with "&" or "U", then there
is an automatic sign extension.
This automatic sign extension is done only if the number of digits
equals a multiple of 16-bits integer (i.e. 4 or 8 digits in hexadecimal,
16 or 32 digits in binary).
So be careful with that horror!
--
Benoît Minisini
More information about the User
mailing list