Adding Hex Values in Visual Basic

     I'm looking to add some Hex values in Visual Basic 2005, for example:

0x40+0x08+0x04+0x02 = 0x4E

I need to have 0x4E as a string in the end so I can include it in a label.

But I have not been able to figure out how to do this. I'm sure it's something simple, but I'm lost... Any help would be appreciated, much thanks...

Who is Participating?
PaulHewsConnect With a Mentor Commented:
Good point about formatting as hex at the end pkumarra... I missed that in the original question

MsgBox(String.Format("Result is 0x{0:X}", IntResult))

>dim tot as long = 0x40+0x08+0x04+0x02

The idea is okay if the hex isn't contained in string, but in Visual Basic, hex digits are represented with &H before them:

Dim Tot As Long = &H40 + &H8 + &H4 + &H2
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim HexArray() As String = {"0x40", "0x08", "0x04", "0x02"}
        Dim IntResult As Integer
        For i As Integer = 0 To HexArray.GetUpperBound(0)
            IntResult += Integer.Parse(HexArray(i).Substring(2), Globalization.NumberStyles.HexNumber)
        MsgBox(String.Format("Result is {0}", IntResult))

    End Sub
End Class
pkumarraConnect With a Mentor Commented:
You can do this

dim tot as long = 0x40+0x08+0x04+0x02
dim str as String =tot.ToString("X")

str will contain the hex representation of tot
CRX4LIFE18Author Commented:
Perfect, thank you very much!!!
All Courses

From novice to tech pro — start learning today.