Hex Conversion


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Hex Conversion

  1. #1
    Philip Morley Guest

    Hex Conversion


    Hi,

    The following statement in VB
    &H8000
    gives a value of -32768, when the actual value is 32768
    (8 * 16 ^3).

    Does anyone have any ideal why VB behaves thus.

    Regards
    Philip

  2. #2
    bob butler Guest

    Re: Hex Conversion


    Philip Morley <philip.morley@bmb.com.bh> wrote in message
    news:39bfde9c$3@news.devx.com...
    >
    > Hi,
    >
    > The following statement in VB
    > &H8000
    > gives a value of -32768, when the actual value is 32768
    > (8 * 16 ^3).
    >
    > Does anyone have any ideal why VB behaves thus.


    because the value fits into a 16bit Integer field but VB interprets the high
    bit as the sign bit so it becomes a negative number. Use CLNG("&H8000") or
    VAL("&H8000&") in expressions or just &H8000& when used as a constant - the
    trailing & forces it to be typed as a 32-bit integer.




  3. #3
    bob butler Guest

    Re: Hex Conversion


    Philip Morley <philip.morley@bmb.com.bh> wrote in message
    news:39bfde9c$3@news.devx.com...
    >
    > Hi,
    >
    > The following statement in VB
    > &H8000
    > gives a value of -32768, when the actual value is 32768
    > (8 * 16 ^3).
    >
    > Does anyone have any ideal why VB behaves thus.


    because the value fits into a 16bit Integer field but VB interprets the high
    bit as the sign bit so it becomes a negative number. Use CLNG("&H8000") or
    VAL("&H8000&") in expressions or just &H8000& when used as a constant - the
    trailing & forces it to be typed as a 32-bit integer.




  4. #4
    Join Date
    Dec 2004
    Posts
    8
    Hope you don't mind me asking a similar question on this thread,


    I'm trying to make a hex converter, it will convert decimal to hex & octal, and oct to decimal & hex no problem, but when it converts hex back to decimal any number above &H7FFFFFFF returns a negative value, &HFFFFFFFF returns -1

    txtDec = CDec("&H" & txtHex)

    how can I get &HFFFFFFFF to return (4294967295) instead of -1
    P.S. I'm working with VB5 and I've tried Bob's suggestions but it didn't work for me

  5. #5
    Join Date
    Aug 2005
    Posts
    3

    Exclamation

    Ta-da. The number &HFFFFFFFF is too big for a long so I used double as a replacement, sinc it can hold bigger numbers, but eats up more memory.

    Code:
    Public Function Hex2Dec(sHex As String) As Double
    
    ' Declare some variables
    Dim sTemp As String
    Dim n As Integer
    Dim x As Double
    x = 0
    
    ' Remove any spare spaces, just incase
    sTemp = Replace(sHex, " ", "")
    
    ' Now the maths starts
    For n = 1 To Len(sTemp)
        x = x + CDbl("&H" & Left$(Right$(sTemp, n), 1)) * _
            (16 ^ (n - 1))
    Next n
    
    ' Return the value!
    Hex2Dec = x
    
    End Function
    This is a VB6 method. It shouldn't be too hard to convert (hopefully). Enjoy
    Last edited by Ultima684; 08-31-2005 at 08:04 PM.

  6. #6
    Join Date
    Dec 2004
    Posts
    8
    The Replace function is VB6 but the idea was great, a few changes and it works great.
    Thanx.
    Last edited by tekdawg; 09-01-2005 at 11:21 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center