I have a string of text being sent through a serial port to a text box (Text1.Text). The string has some symbols in in which I am assuming are Hex values. The hex values are Hex(1), Hex(4), Hex(12), and Hex(17). What I need to do is find the position of these hex symbols in the string of text so that I can split or grab the data after the specific hex symbol. I am not able to use substring or indexof or instr ... or at least I am not using it correctly to find the appropriate symbol.

I was thinking if I need to iterate through the string character by character and grab each symbol or text and get their value??? If it is one of the hex symbols, then get the index number (or position).

Not too sure if I am on the right path, as no matter what I try I cannot find the position of the hex symbols. Any help is certainly appreciated.

You can find the position of a character in a string by using the Instr function. For the search string just substitute Chr(#). For the values you are looking for you would use (respectively)

Chr(1)     or    Chr(&H01)
Chr(4)     or    Chr(&H04)
Chr(18)    or    Chr(&H12)
Chr(23)    or    Chr(&H17)

For example, to look for the first occurrance of the Hex(17) in mystring you could code

Dim pos As Integer = Instr(mystring,Chr(&H17))

If you need to cycle through all of the characteers in a string you can check for a "special" char by

    Dim mystring As String = "abcde" & Chr(4) & "def"

    For i As Integer = 1 To Len(mystring)
        If InStr(Chr(&H1) & Chr(&H4) & Chr(&H18) & Chr(&H23), Mid(mystring, i, 1)) > 0 Then
            MsgBox("hit at char " & i)
        End If
    Next

Edited 4 Years Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.