tmighty
tmighty

Reputation: 11415

Convert hex number to hex value

I have the following code

    Dim sSerialNumber = "9082285F"
    Dim iSerial As Integer = ("&H" & sSerialNumber)
    Dim iSerial2 As Integer = Convert.ToInt32(sSerialNumber, 16)

But VB.NET does not like this. The IDE tells me at runtime that a stack overflow was detected in the line

Dim iSerial As Integer = ("&H" & sSerialNumber)

Does anybody see why and knows how to do that properly?

Thank you!

Upvotes: 2

Views: 117

Answers (1)

Mark Hall
Mark Hall

Reputation: 54562

If you read your error, it stated that you had an exception of System.OverflowException. Try changing your type to Long instead of Integer.

Small Console program to demonstrate.

Sub Main()
    Dim sSerialNumber = "9082285F"
    Dim iSerial As Long = ("&H" & sSerialNumber)
    Dim iSerial2 As Long = Convert.ToInt64(sSerialNumber, 16)
    Console.WriteLine(sSerialNumber)
    Console.WriteLine(iSerial)
    Console.WriteLine(Hex(iSerial))
    Console.WriteLine(iSerial2)
    Console.WriteLine(Hex(iSerial2))
    Console.ReadLine()
End Sub

enter image description here

Upvotes: 5

Related Questions