Working with Hex Value

Hi 2 all,

I have a String that contains Hex Value

Dim hexvalues As String = "AABBCC"

I need to make this addition: AA + BB + CC = 231 (dec: 561)

In a bit word i need a function that it calc Hex Values.

How can i do !



Answer this question

Working with Hex Value

  • gvraovc

    Hi i solved with my code,

    my error was to assing the wrong variable, i assigned it the variable i and i needed variable result


  • KarimRadi

    Dim MyInteger As Integer = Convert.ToInt32("AA", 16)

    Debug.Print(myInteger.ToString) = 170



  • adm10

    But i had to take the HexString in couple of two char

    It mean if i have "AABBCC"

    They are 3 Hex Values:

    • AA
    • BB
    • CC

    So i have to make a Substring too. Am i saying right

    This is my code:

    Dim filex As String = "300000110000002F000900000" & Seem() & Record() & Offset() & "0001" & Value()

    Dim x As String

    Dim result As Integer

    Do While filex.Length >= 1 ' Inner loop.

    x = filex.Substring(0, 2)

    Dim y As Integer = Convert.ToInt32(x, 16)

    result = result + y

    filex = filex.Remove(0, 2)

    Exit Do

    Loop

    Dim risultato As String = Hex(result)

    Dim checksum As StreamWriter

    checksum = File.AppendText(SaveFileDialog1.FileName)

    checksum.Write(risultato)

    checksum.Close()

    What do you think about my code


  • Grotius

    I did something similar try converting it to decimal then add. I did a function Hex2Dec() to convert to decimal just keep expanding. do a do while loop. thats what i did

  • vitagreen

    Ok!

    I tested my function and it return always a Wrong Value :(

    So do you have any ideas or any snippets


  • BrianXXX

    This exactly what i did, but no lucky it didn't work :(

    ' Calcolo Checksum

    Dim filex As String = "AABB"

    Dim temp As Integer

    Dim i As Integer

    For x As Integer = 0 To filex.Length / 2 - 1

    Dim rex As String = filex.Substring(0, 2)

    temp = Convert.ToInt32(rex, 16)

    i = i + temp

    filex = filex.Remove(0, 2)

    Next

    'FineCalcolo Checksum

    Dim risultato = Hex(i)

    The matter is that it doesn't seem working correctly with Substring


  • Klaxas

    Hi,

    You can replace the line highlighted in yellow below with this .>> myValue=&HAA + &HBB + &HCC

    'to deal with HEX values put &H before the characters.

    Enjoy!!

    Use the same idea to subtract or integer division or multiply etc. E.G.

    myValue = &HAB * &HCB

    myValue= &HFAB - &HEAF

    myValue= &HFF \ &H8

     

    Regards,

    S_DS

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim myValue As Int64

    myValue =999999999999999999

    Dim result As String

    result = toHex(myValue)

    TextBox1.Text = result

    End Sub

    Function toHex(ByVal myInt As Int64) As String

    Dim res As String

    Dim Int1, Int2, lg, index As Int64

    Int1 = myInt

    Int2 = myInt

    Try

    lg = Math.Log(Int1) \ Math.Log(16)

    For index = lg To 0 Step -1

    Int2 = Int1 \ (Math.Pow(16, index))

    Int1 = Int1 - (Int2 * (myPow(16, index)))

    Select Case Int2

    Case 0 To 9

    res = res + CStr(Int2)

    Case 10

    res = res + "A"

    Case 11

    res = res + "B"

    Case 12

    res = res + "C"

    Case 13

    res = res + "D"

    Case 14

    res = res + "E"

    Case 15

    res = res + "F"

    Case Is >= 16

    res = toHex(Int2)

    End Select

    Next

    Return res

    Catch ex As Exception

    MsgBox(ex.ToString)

    End Try

    End Function

    Function myPow(ByVal num1 As Int64, ByVal num2 As Int64) As Int64

    Dim n1, n2 As Int64

    n1 = num1 : n2 = num2

    Dim index, result As Int64

    For index = 1 To n2 - 1

    n1 = n1 * num1

    Next

    result = n1

    Return n1

    End Function

     

     



  • Shadi_05

    I solved creating a function.

    My code above is completely wrong. :)


  • BilalShouman

    "I have a String that contains Hex Value"

    Strings do not contain hex values. Strings contain characters. The Characters may represent binary numbers described in a hex radix (base 16). I think these distinctions are important to remember.

    Convert the strings to binary and do with them as you may and then convert them to the radix in which you want them displayed.



  • Working with Hex Value