Searching Byte Array

  • Thread starter Thread starter Larry
  • Start date Start date
L

Larry

I have a Byte Array

Dim A1() as byte = {1,2,3,4,9,9,9,11,12,13,14,9,9,9}

I want to find the location of the first occurance of the byte sequence
{9,9,9}.

Is there a built in Framework class that will do this easily?
I've tried messing with Array.IndexOf but can't seem to get it to
indicate where the byte sequnce starts.

Any help greatly appreciated.

Larry
 
You can convert the byte array into a string of characters then use the
string functions to search, i.e., .Indexof
 
Larry,
| Is there a built in Framework class that will do this easily?

There is no builtin function that I know of per se, however you could create
your own something like:

Public Function FindSequence(ByVal list() As Byte, ByVal value() As Byte)
As Integer
Dim startIndex As Integer = Array.IndexOf(list, value(0))
Do Until startIndex = -1 OrElse list.Length - startIndex <
value.Length
Dim runLength As Integer = 0
For index As Integer = 0 To value.Length - 1
If value(index) <> list(startIndex + index) Then Exit For
runLength += 1
Next
If runLength = value.Length Then Return startIndex
startIndex = Array.IndexOf(list, value(0), startIndex +
runLength)
Loop
Return -1
End Function


Dim A1() As Byte = {1, 2, 3, 4, 9, 9, 9, 11, 12, 13, 14, 9, 9, 9}
Dim value() As Byte = {9, 9, 9}

Dim index As Integer = FindSequence(A1, value)


Hope this helps
Jay


|I have a Byte Array
|
| Dim A1() as byte = {1,2,3,4,9,9,9,11,12,13,14,9,9,9}
|
| I want to find the location of the first occurance of the byte sequence
| {9,9,9}.
|
| Is there a built in Framework class that will do this easily?
| I've tried messing with Array.IndexOf but can't seem to get it to
| indicate where the byte sequnce starts.
|
| Any help greatly appreciated.
|
| Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top