I need to parse the bytes from a file so that I only take the data after a certain sequence of bytes has been identified. For example, if the sequence is simply 0xFF (one byte), then I can use LINQ on 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 multi-byte sequence – e.g. 0xFF, 0xFF – especially one that backtracks in case it starts to get a false positive match?
I’m not aware of any built-in way; as per usual, you can always write your own extension method. Here’s one off the top of my head (there may be more efficient ways to implement it):
I’ll have to check to make sure that this is correct, but it should give you the basic idea; iterate through the elements, track the last sequence of values retrieved, set a flag when the sequence is found, and once the flag is set, start returning each subsequent element.
Edit – I did run a test, and it does work correctly. Here’s some test code: