By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

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

P: n/a

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

Dec 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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

Dec 21 '05 #2

P: n/a
Joergen,

See this complete message from Herfried,

http://groups.google.com/group/micro...225b927c8e1634

I hope this helps,

Cor
Dec 21 '05 #3

P: n/a
"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
Dec 21 '05 #4

P: n/a
>
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/247referenc...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

Dec 21 '05 #5

P: n/a
On Wed, 21 Dec 2005 12:58:42 +0100, "Armin Zingler"
<az*******@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

Dec 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.