How to find a sequence of values ​​(in particular bytes) in a larger collection in .NET.

I need to parse bytes from a file so that I only receive data after a specific sequence of bytes has been identified. For example, if the sequence is just 0xFF (one byte), then I can use LINQ in the collection:

byte[] allBytes = new byte[] {0x00, 0xFF, 0x01};
var importantBytes = allBytes.SkipWhile(byte b => b != 0xFF);
// importantBytes = {0xFF, 0x01}

But is there an elegant way to detect a multibyte sequence - for example, 0xFF, 0xFF - especially the one that returns if it starts to receive a false positive match?

+5
source share
3 answers

; , . ( ):

public static IEnumerable<T> AfterSequence<T>(this IEnumerable<T> source,
    T[] sequence)
{
    bool sequenceFound = false;
    Queue<T> currentSequence = new Queue<T>(sequence.Length);
    foreach (T item in source)
    {
        if (sequenceFound)
        {
            yield return item;
        }
        else
        {
            currentSequence.Enqueue(item);

            if (currentSequence.Count < sequence.Length)
                continue;

            if (currentSequence.Count > sequence.Length)
                currentSequence.Dequeue();

            if (currentSequence.SequenceEqual(sequence))
                sequenceFound = true;
        }
    }
}

, , ; , , , , , .

- , . :

static void Main(string[] args)
{
    byte[] data = new byte[]
    {
        0x01, 0x02, 0x03, 0x04, 0x05,
        0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA
    };
    byte[] sequence = new byte[] { 0x02, 0x03, 0x04, 0x05 };
    foreach (byte b in data.AfterSequence(sequence))
    {
        Console.WriteLine(b);
    }
    Console.ReadLine();
}
+1

, , , , , .

+1

Just like a little theory; This is a regular language issue. You can use the regex engine to detect it. First google hit for "regex in stream" found

http://codeguru.earthweb.com/columns/experts/article.php/c14689

0
source

All Articles