Search Byte []

Finding a string inside a string is very well supported in .NET, but what do you do when the data you need to search is not a string?

I have binary data coming in regular chunks through NetworkStream. Packages are binary, but they all start with a signature sequence of bytes. I am accumulating chunks in a larger buffer and looking for a signature to start the package.

What I'm really looking for is equivalent to a byte[]method String.IndexOf(ss). I have an unpleasant feeling that I will have to implement this on my own using a loop and a final machine.

Any suggestions? To you!


As suggested, Array.IndexOf (byte) will at least save me an explicit loop. Starting with the publication, it occurred to me to find the first byte of the signature, and then trace forward to match where the last byte of the signature should be, then if they both match, try to compare the brute force for the rest of the line. The advantage of this approach is that it cheaply rejects false matches and allows me to cheaply reject when I have a partial signature waiting for another fragment.

Google shows that the above brilliant plan is a degenerate case of the KMP or Knuth-Morris-Pratt algorithm. On the bright side, if Knut put his name on him, he probably smeared lightning, on the other hand, why is everything, when I have a good idea, Donald Knut thought about it 25 years ago?

, , .

+5
3

Array.IndexOf, .

, . , , . , .

TCP, , :)

+3

, , Boyer-Moore Boyer-Moore (, ). mime Java. code .Net( LGPL).

+2

/ ? , , , . . .

. . . , base64string, simepl ( KeyedCollection, ) ..

0

All Articles