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

  • Thread starter Thread starter Joergen Bech
  • Start date Start date
J

Joergen Bech

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
 
Joergen said:
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)
 
Joergen Bech @ post1.tele.dk> said:
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
 
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/247reference/msgs/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
 
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
 
Back
Top