Results 1 to 33 of 33

Thread: WHat's up with VB6 and hex numbers ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66

    WHat's up with VB6 and hex numbers ?

    Why does VB6 do sign extension with hex numbers ???
    Even if I use CLng(&hFFFF), VB displays -1

    text3.text = "&HFFFF"
    Label3.Caption = Str(CLng(Val(Text3.Text)))

    FFFF is small enough where it should not cause sign extesnion, when referenced as a Long.

    IS there another VB equivalent of typecasting ?

  2. #2
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    Have you tried referencing it as a double?
    If wishes were fishes we'd all cast nets.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by Graff
    Have you tried referencing it as a double?
    I think so. Is this what you mean -> CDBL() ??

    All three of these text boxes contain &HFFFF

    Label1.Caption = Str(Val(Text1.Text))
    Label2.Caption = Str(CLng(Val(Text2.Text)))
    Label3.Caption = Str(CDbl(Val(Text3.Text)))

    And all three labels show -1

  4. #4
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    Tulsa,Ok
    Posts
    262
    You cant include the &H into the text part of your text boxes, the compilier will think they are just text.

    Place three labels and three text boxes on your form and past this code. this should work for you.

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.   Text1.Text = "FFFF"
    4.   Text2.Text = "FFFFF"
    5.   Text3.Text = "FFFFFF"
    6.  
    7.   Label1.Caption = CLng("&H" & Text1.Text)
    8.   Label2.Caption = CLng("&H" & Text2.Text)
    9.   Label3.Caption = CLng("&H" & Text3.Text)
    10.  
    11. End Sub

  5. #5
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    Originally posted by Phenglai
    You cant include the &H into the text part of your text boxes, the compilier will think they are just text.

    Place three labels and three text boxes on your form and past this code. this should work for you.
    Sorry but I don't get the difference between:
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.   Text1.Text = "FFFF"
    4.   Text2.Text = "FFFFF"
    5.   Text3.Text = "FFFFFF"
    6.  
    7.   Label1.Caption = CLng("&H" & Text1.Text)
    8.   Label2.Caption = CLng("&H" & Text2.Text)
    9.   Label3.Caption = CLng("&H" & Text3.Text)
    10.  
    11. End Sub

    And

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.   Text1.Text = "&HFFFF"
    4.   Text2.Text = "&HFFFFF"
    5.   Text3.Text = "&HFFFFFF"
    6.  
    7.   Label1.Caption = CLng(Text1.Text)
    8.   Label2.Caption = CLng(Text2.Text)
    9.   Label3.Caption = CLng(Text3.Text)
    10.  
    11. End Sub

  6. #6
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    Tulsa,Ok
    Posts
    262
    Not much, personal preference really

  7. #7
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    Ok, your statement that "You cant include the &H into the text part of your text boxes, the compilier will think they are just text" (plus the fact that your code reflected that) confused me. A string is a string and whether it is concatenated or not will make no difference in converting hex to decimal.

  8. #8
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    Tulsa,Ok
    Posts
    262
    I guess I wasn't clear on what I was trying to say. They had

    VB Code:
    1. CLng(&HFFFF)

    and I was trying to say that for it to be a string for the compilier to recognize it

    VB Code:
    1. CLng("&HFFFF")

    They needed to add the " "s in there so it would recognize it as a string.

    Just didn't explain myself very well did I

  9. #9
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    BTW, cappy.

    If I use this code:

    VB Code:
    1. Dim s1 As String
    2.     Dim s2 As String
    3.     Dim s3 As String
    4.    
    5.     s1 = "&HFFF"
    6.     s2 = "&HFFFF"
    7.     s3 = "&HFFFFF"
    8.    
    9.     Debug.Print CStr(CLng(s1))
    10.     Debug.Print CStr(CLng(s2))
    11.     Debug.Print CStr(CLng(s3))

    it will yield the results:

    4095
    65535
    1048575

    Will that work for you?

  10. #10
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    OK, right, Phenglai. I gotcha now.

    I hope either your method or mine should help cappy anyway.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by BestCoder
    BTW, cappy.

    If I use this code:

    VB Code:
    1. Dim s1 As String
    2.     Dim s2 As String
    3.     Dim s3 As String
    4.    
    5.     s1 = "&HFFF"
    6.     s2 = "&HFFFF"
    7.     s3 = "&HFFFFF"
    8.    
    9.     Debug.Print CStr(CLng(s1))
    10.     Debug.Print CStr(CLng(s2))
    11.     Debug.Print CStr(CLng(s3))

    it will yield the results:

    4095
    65535
    1048575

    Will that work for you?
    I'm not sure yet- but at least it gives me another viewpoint.

    I'm still trying to understand why &HFFFF gets converted to -1
    in this situation

    Dim doubnum As Double
    Dim longnum As Long

    doubnum = CDbl(&HFFFF)
    longnum = CLng(&HFFFF)

    Both values are = -1, when I break and evaluate the expression.

    Is there a VB hex FAQ somewhere ? :-)))

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    [QUOTE]Originally posted by BestCoder
    [B]BTW, cappy.

    If I use this code:

    VB Code:
    1. Dim s1 As String
    2.     Dim s2 As String
    3.     Dim s3 As String
    4.    
    5.     s1 = "&HFFF"
    6.     s2 = "&HFFFF"
    7.     s3 = "&HFFFFF"
    8.    
    9.     Debug.Print CStr(CLng(s1))
    10.     Debug.Print CStr(CLng(s2))
    11.     Debug.Print CStr(CLng(s3))

    it will yield the results:

    4095
    65535
    1048575

    dim d1 as double
    dim L1 as long

    d1= &HFFFF
    L1= &HFFFF

    printing these both yield -1.
    WHY ??

  13. #13
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    If you haven't found your answer by tomorrow morning, I'll look at it again... my brain is getting fried right now.

  14. #14
    Fanatic Member Graff's Avatar
    Join Date
    Jan 2002
    Location
    Calgary
    Posts
    668
    DOH! Add a & after it haha I knew it was something simple.

    Dim l1 as long

    l1 = &HFFFF&
    If wishes were fishes we'd all cast nets.

  15. #15
    Addicted Member
    Join Date
    Sep 2001
    Location
    Canada
    Posts
    202
    Oh right! I'm trying to remember how the signs work with hex, because &HFFF0 to &HFFFF is -16 to -1, just as &HFFFFFFF0 to &HFFFFFFFF is. I know it's something to with having negative numbers but I'm not sure how yet. I must look that up.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by Graff
    DOH! Add a & after it haha I knew it was something simple.

    Dim l1 as long

    l1 = &HFFFF&
    THANKS !

    But What does the 2nd & Do ?
    Is this a documented feature ???

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by BestCoder
    Oh right! I'm trying to remember how the signs work with hex, because &HFFF0 to &HFFFF is -16 to -1, just as &HFFFFFFF0 to &HFFFFFFFF is. I know it's something to with having negative numbers but I'm not sure how yet. I must look that up.
    Wouldn't that depend on the data type bing integer, or long ?
    Does VB just sign extend everything ???

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by Graff
    DOH! Add a & after it haha I knew it was something simple.

    Dim l1 as long

    l1 = &HFFFF&
    Ok- so now I know ho to prevent hex numbers from being converted to negative values, by using a trailing &, as in

    Dim L1 as Long

    L1=&HFF&
    (evaluates as 255)


    L1=&HFF
    (evaluates as -1)


    L1=&HFFFF&
    (evaluates as 65535)

    L1=&HFFFF
    (evaluates as -1)


    Since we are using a long data type in all of the assignmnets, why does VB represent the number as negative, when you don't use a trailing & ?

    Is the trailing & just preventing sign extension ?

    What is really happening behind the scenes ??

  19. #19
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    dont worry about a trailing '&' as being the issue. It is all to do with how integers, longs etc are stored as negative or positive..

    FFFF is 16 x 16 x 16 x 16 = 65536 which is the maximum 'capacity' of an integer. However, integers go from - 32768 to 32767. The first bit is used to show whether the number is positive or negative in that range... ie from 0 to 7 in the first bit gives u positive values and from 8 to F are for negative values.

    &h0000 to &h7FFF = 0 to 32767
    &h8000 to &hFFFF = -32768 to -1

    The same concept is used with longs.

    &h0000000 to &h7FFFFFFF = 0 to 2147483647
    &h8000000 to & hFFFFFFF = -2147483648 to -1
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by beachbum
    Hi
    dont worry about a trailing '&' as being the issue. It is all to do with how integers, longs etc are stored as negative or positive..

    FFFF is 16 x 16 x 16 x 16 = 65536 which is the maximum 'capacity' of an integer. However, integers go from - 32768 to 32767. The first bit is used to show whether the number is positive or negative in that range... ie from 0 to 7 in the first bit gives u positive values and from 8 to F are for negative values.

    &h0000 to &h7FFF = 0 to 32767
    &h8000 to &hFFFF = -32768 to -1

    The same concept is used with longs.

    &h0000000 to &h7FFFFFFF = 0 to 2147483647
    &h8000000 to & hFFFFFFF = -2147483648 to -1

    >>dont worry about a trailing '&' as being the issue.
    You have no choice but to use them in VB, because Microsoft stuck us with with signed data types in VB.

    If you don't use the trailing &, your logical tests will NOT come out correctly, among other problems.

  21. #21
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    I dont see how ur logical tests can come out wrong if u know that both values are either signed or unsigned... it would only be an issue if u were logically comparing signed and unsigned values.

    Ur original question was why &HFFFF became -1. Now u know
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by beachbum
    I dont see how ur logical tests can come out wrong if u know that both values are either signed or unsigned... it would only be an issue if u were logically comparing signed and unsigned values.

    Ur original question was why &HFFFF became -1. Now u know
    Try this, it should explain it all. I've put one button, and one label on the form, but you dont even need them,. You could to this at form_load time to make it simpler.


    Private Sub Command1_Click()
    Dim L1 As Long
    Dim L2 As Long


    L1 = &HFFFF
    L2 = &HFFFF&

    If (L1 = L2) Then
    Label1.Caption = "L1 = L2"
    End If

    If (L1 > L2) Then
    Label1.Caption = "L1 > L2"
    End If

    If (L1 < L2) Then ' ONLY this test is true
    Label1.Caption = "L1 < L2" ' this is what is displayed in label1
    End If


    End Sub

  23. #23
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    What?

    Code:
    Dim L1 as Long 
    
    L1=&HFF& 
    '(evaluates as 255) 
    
    
    L1=&HFF 
    '(evaluates as -1) 
    
    
    L1=&HFFFF& 
    '(evaluates as 65535) 
    
    L1=&HFFFF 
    '(evaluates as -1)
    I don't think that L1=&hFF equals -1??? Vb stores 2 bytes, so in this case &hff or &hff& always equals 255 and NOT -1!!!
    Even if L1 was of type "Byte" it would still remain 255!

  24. #24
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by cappy2112
    Try this, it should explain it all. I've put one button, and one label on the form, but you dont even need them,. You could to this at form_load time to make it simpler.
    Originally posted by beachbum
    I dont see how ur logical tests can come out wrong if u know that both values are either signed or unsigned... it would only be an issue if u were logically comparing signed and unsigned values.
    'Nuff said
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    >>I don't think that L1=&hFF equals -1??? Vb stores 2 bytes, so >>in this case &hff or &hff& always equals 255 and NOT -1!!!
    >>Even if L1 was of type "Byte" it would still remain 255! [/B]

    Dont think !
    Type the code in, let VB show you how it is treating the numbers !

    This is what matters, not what you think is happening !

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Originally posted by beachbum
    'Nuff said
    Dont think !
    Type the code in, let VB show you how it is treating the numbers !

    This is what matters, not what you think is happening !

    Just copy and paste the code I've posted above, it into a program,
    before you prove you dont know what you're talking about, again.

  27. #27
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Oh gawd someone help me please!!! Your code is way too complicated for me to key in. I am still looking for the Any key and so will be a while before I find that Command1 thing. If you bother to read what I said instead of promoting your work of genius you will note that I said you would have no problems unless u were comparing signed values with unsigned values... and that is exactly what your vast production does. And you arent even using logical operators at all yet commenting as if you are. I dont need some wannabe fool telling me I dont know what I am talking about... there are plenty of others that will do that gladly
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    San Jose, CA
    Posts
    66
    Throughout all of the this arguing, NOONE has explained what the trailing & does (as Microsoft would define it), or where it is documented in VB.

    I can see what it is doing, and guess what's really happening under the covers, but I want to KNOW what it's called, and where it is documented (if at all).

  29. #29
    New Member
    Join Date
    Oct 2014
    Posts
    1

    Re: WHat's up with VB6 and hex numbers ?

    Thanks for the help guys! I can't believe my employer is still using VB 6....I should not have needed to find a forum from 2002...

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

    Re: WHat's up with VB6 and hex numbers ?

    Quote Originally Posted by cappy2112 View Post
    Throughout all of the this arguing, NOONE has explained what the trailing & does (as Microsoft would define it), or where it is documented in VB.
    Google is your friend: vb6 hex number declaration

    http://msdn.microsoft.com/en-us/library/s9cz43ek.aspx

    [edit] this sentence is key:
    Literals can also use the identifier type characters (%, &, @, !, #, $), as can variables, constants, and expressions. However, the literal type characters (S, I, L, D, F, R, C) can be used only with literals.
    [/edit]
    Last edited by szlamany; Oct 16th, 2014 at 03:10 PM.

    *** 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

  31. #31
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    764

    Re: WHat's up with VB6 and hex numbers ?

    Since we are using a long data type in all of the assignments ...
    Sure you are, but is there anything in your expression that tells VB that you're in any way, shape or form interested in Long values? No.

    Faced with nothing to tell it otherwise, VB will pick the efficient option and stick to purely Integer arithmetic, hence your sign extension problem.

    The Type Declaration Character, "&", tells VB that you explicitly want to work with a Long value at this point.

    Regards, Phill W.

  32. #32
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: WHat's up with VB6 and hex numbers ?

    This may already be answered as I didn't read everything, but &HFFFF is an integer (not a long). In fact, it's the integer = -1. You convert this integer to a long with CLng and you're just converting -1 as an integer to -1 as a long. If you want to truly have a hex LONG, then use the "&" declaration after the number. For instance, Debug.Print CLng(&HFFFF&) will print 65535. Or even Debug.Print &HFFFF& will print 65535, whereas Debug.Print &HFFFF will print -1. I hope this helps.

    As a final thought, literals and also undeclared constants always go to the lowest possible type, unless specifically forced with one of the &!#%$@ type declarations.
    Last edited by Elroy; Oct 17th, 2014 at 02:40 PM.

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

    Re: WHat's up with VB6 and hex numbers ?

    Quote Originally Posted by szlamany View Post
    Google is your friend: vb6 hex number declaration

    http://msdn.microsoft.com/en-us/library/s9cz43ek.aspx

    [edit] this sentence is key: [/edit]
    That's what the MSDN link I talks about.

    *** 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

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