Working with Hex Value

Hi 2 all,

I have a String that contains Hex Value

Dim hexvaluesAs 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?!

[421 byte] By [flash.tato] at [2007-12-28]
# 1
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
IceAngel89 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

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

ReneeC at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

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

flash.tato at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

I solved creating a function.

My code above is completely wrong. :)

flash.tato at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Ok!

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

So do you have any ideas or any snippets?

flash.tato at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

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

Debug.Print(myInteger.ToString) = 170

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7

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

flash.tato at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

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

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9

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

flash.tato at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...