PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET 8- or 10-character hex string to signed 32-bit Integer value - any better ways?

Reply

8- or 10-character hex string to signed 32-bit Integer value - any better ways?

 
Thread Tools Rate Thread
Old 21-12-2005, 11:15 AM   #1
Joergen Bech
Guest
 
Posts: n/a
Default 8- or 10-character hex string to signed 32-bit Integer value - any better ways?



Basically, I want to convert hex values in the range
"00000000" to "FFFFFFFF" to a signed, 32-bit Integer
value.

In VB6, I could just write lngValue = Val(hexstring$).

In VB.Net, I seem to be forced to do something like

---snip---
Private Function HexToInteger(ByVal hexValue As String) As Integer
If hexValue.ToLower.StartsWith("&h") Then
hexValue = hexValue.Substring(2)
End If
Dim value As Long = Long.Parse(hexValue,
Globalization.NumberStyles.HexNumber)
If value > Integer.MaxValue Then
value = value - 4294967296&
End If
Return CType(value, Integer)
End Function
---snip---

Have I overlooked a function in the framework?

Surely there is a faster/shorter/better way?

TIA,

Joergen Bech



  Reply With Quote
Old 21-12-2005, 11:27 AM   #2
Larry Lard
Guest
 
Posts: n/a
Default Re: 8- or 10-character hex string to signed 32-bit Integer value - any better ways?


Joergen Bech wrote:
> Basically, I want to convert hex values in the range
> "00000000" to "FFFFFFFF" to a signed, 32-bit Integer
> value.
>
> In VB6, I could just write lngValue = Val(hexstring$).
>
> In VB.Net, I seem to be forced to do something like
>
> ---snip---
> Private Function HexToInteger(ByVal hexValue As String) As Integer
> If hexValue.ToLower.StartsWith("&h") Then
> hexValue = hexValue.Substring(2)
> End If
> Dim value As Long = Long.Parse(hexValue,
> Globalization.NumberStyles.HexNumber)
> If value > Integer.MaxValue Then
> value = value - 4294967296&
> End If
> Return CType(value, Integer)
> End Function
> ---snip---
>
> Have I overlooked a function in the framework?
>
> Surely there is a faster/shorter/better way?


Well, you found Long.Parse but you missed Integer.Parse

Dim s1 As String = "1234"
Dim s2 As String = "FEDCBA98"

Dim i1 As Integer = Integer.Parse(s1, _
Globalization.NumberStyles.HexNumber)
Dim i2 As Integer = Integer.Parse(s2, _
Globalization.NumberStyles.HexNumber)

--
Larry Lard
Replies to group please

  Reply With Quote
Old 21-12-2005, 11:40 AM   #3
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: 8- or 10-character hex string to signed 32-bit Integer value - any better ways?

Joergen,

See this complete message from Herfried,

http://groups.google.com/group/micr...1225b927c8e1634

I hope this helps,

Cor


  Reply With Quote
Old 21-12-2005, 11:58 AM   #4
Armin Zingler
Guest
 
Posts: n/a
Default Re: 8- or 10-character hex string to signed 32-bit Integer value - any better ways?

"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> schrieb
>
> Basically, I want to convert hex values in the range
> "00000000" to "FFFFFFFF" to a signed, 32-bit Integer
> value.


return System.Convert.ToInt32(hexvalue, 16)


Armin
  Reply With Quote
Old 21-12-2005, 12:08 PM   #5
Joergen Bech
Guest
 
Posts: n/a
Default Re: 8- or 10-character hex string to signed 32-bit Integer value - any better ways?

>
>Well, you found Long.Parse but you missed Integer.Parse
>
> Dim s1 As String = "1234"
> Dim s2 As String = "FEDCBA98"
>
> Dim i1 As Integer = Integer.Parse(s1, _
>Globalization.NumberStyles.HexNumber)
> Dim i2 As Integer = Integer.Parse(s2, _
>Globalization.NumberStyles.HexNumber)


Thanks. Knew I had overlooked something. Started off with
CInt, which does not handle 80000000-FFFFFFFF very well.

Because of that observation, along with this thread
http://www.dotnet247.com/247referen.../58/293488.aspx
I was thrown off course and thought I had to make use of
the Long (64-bit) data type in my solution - never checking if
Integer.Parse(...HexNumber) worked correctly. Which it does.

Regards,

Joergen Bech



  Reply With Quote
Old 21-12-2005, 12:18 PM   #6
Joergen Bech
Guest
 
Posts: n/a
Default Re: 8- or 10-character hex string to signed 32-bit Integer value - any better ways?

On Wed, 21 Dec 2005 12:58:42 +0100, "Armin Zingler"
<az.nospam@freenet.de> wrote:

>"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> schrieb
>>
>> Basically, I want to convert hex values in the range
>> "00000000" to "FFFFFFFF" to a signed, 32-bit Integer
>> value.

>
>return System.Convert.ToInt32(hexvalue, 16)
>
>
>Armin


Excellent. Even better than Integer.Parse(...HexNumber).

Now, if only System.ParseNumbers was exposed so I could
skip the base check ...

But this is good enough for me.

Regards,

Joergen Bech



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off