PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
8- or 10-character hex string to signed 32-bit Integer value - any better ways?
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
8- or 10-character hex string to signed 32-bit Integer value - any better ways?
![]() |
8- or 10-character hex string to signed 32-bit Integer value - any better ways? |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: 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 |
|
|
|
#2 |
|
Guest
Posts: 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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Joergen,
See this complete message from Herfried, http://groups.google.com/group/micr...1225b927c8e1634 I hope this helps, Cor |
|
|
|
#4 |
|
Guest
Posts: 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 |
|
|
|
#5 |
|
Guest
Posts: 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/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 |
|
|
|
#6 |
|
Guest
Posts: n/a
|
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 |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 


