简体   繁体   中英

Finding a byte array in C#

I was wondering what would be the best approach towards finding the index of a sub-array from within another byte array in C#. For example if I have the following as my main array.

byte[] inputArray = {0xFF,0xDD,0xA,0xF,0x1,0x2,0x78,0x05,0x00,0x01};
byte[] tobeFound = {0x78,0x05};

Now I want to find the byte array "tobeFound" inside inputArray. And I should get the index 6. This is a short example. Both arrays can be very large.

So what should I go for? LINQ, Array.IndexOf ?? I need to have good performance as well.

Thanks for any pointers and sharing some experience!

You can use the well-tested Boyer-Moore string search algorithm , since you are essentially searching a string (the bytes can be considered characters).

Here is what appears to be a decent implementation of Boyer-Moore in C# for strings (along with turbo Boyer-Moore and another I'd never heard of). Converting these to use Byte[] should be trivial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM