简体   繁体   English

在C#中查找字节数组

[英]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#. 我想知道从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. 现在,我想在inputArray中找到字节数组“ tobeFound”。 And I should get the index 6. This is a short example. 我应该得到索引6。这是一个简短的示例。 Both arrays can be very large. 两个阵列都可能非常大。

So what should I go for? 那我该怎么办呢? LINQ, Array.IndexOf ?? 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). 您可以使用经过良好测试的Boyer-Moore字符串搜索算法 ,因为您本质上是在搜索字符串(可以将字节视为字符)。

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). 这似乎是C#中Boyer-Moore对字符串的不错实现 (以及Turbo Boyer-Moore和我从未听说过的另一个)。 Converting these to use Byte[] should be trivial. 将它们转换为使用Byte[]应该很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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