Results 1 to 14 of 14

Thread: [RESOLVED] I thought &H was shorthand for hex?

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [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:
    1. 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:
    1. Dim Something As UInteger = 4294967295

    it works fine.

    So how come the &H version won't work?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  3. #3
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    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)
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: I thought &H was shorthand for hex?

    Quote Originally Posted by 4x2y View Post
    Add & suffix
    &HFFFFFFFF& = 4294967295 because it is represent unsigned integer
    Actually that would represent a 64 bit signed integer (Long)
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: I thought &H was shorthand for hex?

    Quote Originally Posted by Milk View Post
    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



  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: I thought &H was shorthand for hex?

    I like to use the &H annotation when doing BitWise enums like:
    vb Code:
    1. <Flags()> _
    2. Friend Enum WeekDays
    3.     None = &H0
    4.     Sunday = &H1
    5.     Monday = &H2
    6.     Tuesday = &H4
    7.     Wednesday = &H8
    8.     Thursday = &H10
    9.     Friday = &H20
    10.     Saturday = &H40
    11. End Enum
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: I thought &H was shorthand for hex?

    Quote Originally Posted by JuggaloBrotha View Post
    I like to use the &H annotation when doing BitWise enums like:
    vb Code:
    1. <Flags()> _
    2. Friend Enum WeekDays
    3.     None = &H0
    4.     Sunday = &H1
    5.     Monday = &H2
    6.     Tuesday = &H4
    7.     Wednesday = &H8
    8.     Thursday = &H10
    9.     Friday = &H20
    10.     Saturday = &H40
    11. End Enum
    Although i'm not quite sure what the relevancy is for this post in the context of chris128's question
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  9. #9

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. 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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  11. #11
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    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)
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  12. #12

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: I thought &H was shorthand for hex?

    Quote Originally Posted by AceInfinity View Post
    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?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: I thought &H was shorthand for hex?

    Quote Originally Posted by szlamany View Post
    @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:
    1. Dim a As UInt64
    2.         a = &HFFFFFFFFFFFFFFFF& ' Error
    3.         a = &HFFFFFFFFFFFFFFFFUL ' OK



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width