Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joergen Bech <jbech<NOSPAM>@ - 21 Dec 2005 11:15 GMT
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
Larry Lard - 21 Dec 2005 11:27 GMT
Joergen Bech wrote:
> Basically, I want to convert hex values in the range
> "00000000" to "FFFFFFFF" to a signed, 32-bit Integer
[quoted text clipped - 21 lines]
>
> 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)

Signature

Larry Lard
Replies to group please

Joergen Bech <jbech<NOSPAM>@ - 21 Dec 2005 12:08 GMT
>Well, you found Long.Parse but you missed Integer.Parse :(
>
[quoted text clipped - 5 lines]
>        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
Cor Ligthert [MVP] - 21 Dec 2005 11:40 GMT
Joergen,

See this complete message from Herfried,

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/c1225b92
7c8e1634


I hope this helps,

Cor
Armin Zingler - 21 Dec 2005 11:58 GMT
"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
Joergen Bech <jbech<NOSPAM>@ - 21 Dec 2005 12:18 GMT
>"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNOSPAM> schrieb
>>
[quoted text clipped - 5 lines]
>
>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.