Need to search array of byte for sub-array
Posted on 2006-06-04
I am reading a binary file into a one dimensional array of byte. Now what I'd like to do is search this array for a sub-array of byte. In other words, this would be the equivilent of String.IndexOf(). For some reason I just can't seem to wrap my head around this. (Performance is not a huge issue for my application. ) If anyone could post an example of how to accomplish this, it would be most appreciated.
Here is what I tried, but it always returns 0.
private int IndexOf(byte searchIn, byte searchFor)
for (int i = 0; i < searchIn.Length; i++)
bool match = true;
for (int j = 0; j < searchFor.Length; j++)
int test = searchIn[i];
if (searchIn[i] != searchFor[j])
match = false;
if (match) return i;