[RESOLVED] I thought &H was shorthand for hex?
I thought if you saw something like this:
0xffffffff
you could simply replace the 0x with &H in VB.NET and it would be exactly the same thing? I've always believed that and it has always worked for me, up until I tried that example above. If I do this:
vb.net Code:
Dim Something As UInteger = &Hffffffff
I get an error telling me that a UInteger cannot hold that value. However, if I stick 0xffffffff in a hex to decimal converter it equals 4294967295 and if I do this:
vb.net Code:
Dim Something As UInteger = 4294967295
it works fine.
So how come the &H version won't work? :)
Re: I thought &H was shorthand for hex?
Add & suffix
&HFFFFFFFF = -1 because it is represent signed integer
&HFFFFFFFF& = 4294967295 because it is represent unsigned integer
Re: I thought &H was shorthand for hex?
&Hffffffff is interpretted as an Integer value, in which the max for that type would be: 2147483647, therefore, &Hffffffff evaluates down to -1 which can't be expressed by unsigned integer.
See for yourself:
Code:
Dim x As Integer = &HFFFFFFFF
Console.WriteLine(x)
Re: I thought &H was shorthand for hex?
Quote:
Originally Posted by
4x2y
Add & suffix
&HFFFFFFFF& = 4294967295 because it is represent unsigned integer
Actually that would represent a 64 bit signed integer (Long)
Re: I thought &H was shorthand for hex?
I don't use VB.Net but... does the UI suffix work?
Dim Something As UInteger = &HFFFFFFFFUI
VB classic used a bunch of symbols for literals, & ! @ $ etc. I'm guessing that VB.Net has carried these over. I think there are also alternatives that are easier to remember and fit better with other languages, S US UI L UL F R D etc.
Re: I thought &H was shorthand for hex?
Quote:
Originally Posted by
Milk
I don't use VB.Net but... does the UI suffix work?
Yes, it works, Debug.Print(&HFFFFFFFFUI) print 4294967295
I used to use & suffix because i come from VB6 :D
Re: I thought &H was shorthand for hex?
I like to use the &H annotation when doing BitWise enums like:
vb Code:
<Flags()> _
Friend Enum WeekDays
None = &H0
Sunday = &H1
Monday = &H2
Tuesday = &H4
Wednesday = &H8
Thursday = &H10
Friday = &H20
Saturday = &H40
End Enum
Re: I thought &H was shorthand for hex?
Quote:
Originally Posted by
JuggaloBrotha
I like to use the &H annotation when doing BitWise enums like:
vb Code:
<Flags()> _
Friend Enum WeekDays
None = &H0
Sunday = &H1
Monday = &H2
Tuesday = &H4
Wednesday = &H8
Thursday = &H10
Friday = &H20
Saturday = &H40
End Enum
Although i'm not quite sure what the relevancy is for this post in the context of chris128's question :confused:
Re: I thought &H was shorthand for hex?
haha yeah it didn't answer my question but it was kind of relevant :)
So the problem then is that &H always denotes an Integer and an Integer cannot hold that number only a UInteger can. That makes sense, but the thing that confused me is that the error message you get when you try this:
vb Code:
Dim Something As UInteger = &Hffffffff
is "Constant expression not representable in type 'UInteger'"
I can understand the reasoning for that now after reading your explanations, as like you said the constant I specified actually equals -1 when you treat it as an integer, but it confused be as I thought it was complaining that the value was too large for a UInt. Anyway, thanks for the explanation and the tip on using the UI suffix :)
Re: [RESOLVED] I thought &H was shorthand for hex?
At a guess this behaviour is another throwback from VB classic. VB classic has no unsigned types other than byte so hex is always treated as signed.
C# is the opposite, hex literals are treated as unsigned if the high bit is set.
Re: [RESOLVED] I thought &H was shorthand for hex?
Quote:
Originally Posted by chris128
So the problem then is that &H always denotes an Integer and an Integer cannot hold that number only a UInteger can
Mmmm--Not quite, not in this example anyways. An Integer can hold a value of -1, but a UInteger cannot, so you have it backwards, unless i'm reading that wrong...
You can get around the limitation that the &H prefix can hold by defining the value as an Int64 value (Long) in the form of hex by what 4x2y had originally suggested. He said the "&" was for UInteger, but that's not quite right, the "&" suffix actually is a type character for Long which is a 64 bit signed integer. (Double the max of an Int32 value)
Re: [RESOLVED] I thought &H was shorthand for hex?
Oh yeah sorry I wrote it the wrong way around (its been a very long day), but I do understand :) thanks, and yeah I'm using the UI suffix now to force the number to be a UInt rather than using the & suffix to force it to be an Int64.
Re: I thought &H was shorthand for hex?
Quote:
Originally Posted by
AceInfinity
Actually that would represent a 64 bit signed integer (Long)
@4x2y - do you realize it appears unsigned because you entered a 64 bit world where the sign bit is a "bit" higher up?
Re: I thought &H was shorthand for hex?
Quote:
Originally Posted by
szlamany
@4x2y - do you realize it appears unsigned because you entered a 64 bit world where the sign bit is a "bit" higher up?
I see.
It is better to use UI and UL suffix because & suffix will not help with UInt64
vb Code:
Dim a As UInt64
a = &HFFFFFFFFFFFFFFFF& ' Error
a = &HFFFFFFFFFFFFFFFFUL ' OK