-
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
-
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.
-
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.
-
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
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks