Find location of a short sequence of bytes in byte array
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.
You may use Boyer-Moore's algorithm, which is better than linear in the average.
HTH!
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.
精彩评论