Home > Net >  Calculating (add) HEX values
Calculating (add) HEX values

Time:07-26

i am trying to do the following:Looking up values in a .txt file, such as "DD" which will be written into a string variable, and then converted into a Byte variable. I then want to add to this, for example BB 10 = CB

For testing, my code is the following

Public reqw As String = "BB" 'normally comes from the txt. file
Public btansw as byte
Public finansw as string 

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim MyByte As Byte
    If Byte.TryParse(reqw, System.Globalization.NumberStyles.AllowHexSpecifier, Nothing, MyByte) Then
        btansw = MyByte 
    Else
        RichTextBox3.Text &= "Err2"

    End If 

This code converts the string "BB" into bytes. The acutal problem is the following:

finansw = Microsoft.VisualBasic.Conversion.Hex(btansw  10) 

    RichTextBox3.Text &= finansw

This gives me "C5" as result in the RichTextBox but it is supposed to be "CB".

Can someone explain what i am doing wrong?

CodePudding user response:

You are adding 10, i.e. ten, so the result is correct. If what you actually want to add is 0x10, i.e. sixteen, then you need to actually use a hex value. In VB, you use the &H prefix to indicate a hex literal.

finansw = (btansw   &H10).ToString("X2")

Let's not write our VB.NET like we were still using VB6.

  • Related