0

How can I efficiently find the location of the first instance of a four byte sequence within a byte array? Is there something more efficient than looping through the whole array, or a built-in method?

I'm searching through a byte array for a pair of CrLF. I can't convert it to string for an InStr first because I need the position of it in the original byte array.

I am trying to figure out the location of this, as it delimits between a string portion and a binary portion, similar (well, exactly like for this part of it) to an HTTP header. There is a string portion, and two CrLf before the content begins.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • You'll need to convert some examples to vb (or just use them in a referenced C# project), but some good alternatives are here: http://stackoverflow.com/questions/283456/byte-array-pattern-search. – rsenna Jan 15 '11 at 22:19

2 Answers2

1

Assuming nothing about the data set (sorted/special ordering) the best you can do is an O(n) algorithm, which means looking through the whole array once.

helloworld922
  • 10,801
  • 5
  • 48
  • 85
1

You may use Boyer-Moore's algorithm, which is better than linear in the average.

HTH!

Community
  • 1
  • 1
Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190