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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CRX4LIFE18Author Commented:
Perfect, thank you very much!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.