C#中如何不断找到一个字节数组内的字节数组? [英] c# How to Continuously find a byte array inside a byte array?
问题描述
我试图找到连续字节数组中的字节数组(字节[]),我发现只能找到第一次出现一个代码。
< STRONG>这是我找到的代码: 查找阵列(另一个阵列内的byte [])
问:??如何不断找到下面这段代码的字节数组
公众诠释SearchBytes(字节[]草垛,字节[]针)
{
INT LEN =针。长度;
INT上限= haystack.Length - LEN;
的for(int i = 0; I< =限制;我++)
{
时int k = 0;
为(; K< LEN; k ++)
{
如果(针[K] =草垛[I + K]!)打破;
}
如果(K == LEN)回报我;
}
返回-1;
}
您可以更改方法接受起始索引是这样的:
公众诠释SearchBytes(字节[]草垛,字节[]针,INT START_INDEX)
{
INT LEN = needle.Length;
INT上限= haystack.Length - LEN;
的for(int i = START_INDEX; I< =限制;我++)
{
时int k = 0;
为(; K< LEN; k ++)
{
如果(针[K] =草垛[I + K]!)打破;
}
如果(K == LEN)回报我;
}
返回-1;
}
所不同的仅仅是,这个方法接受一个 START_INDEX
并开始在这个特定的索引搜索
现在,你可以用这样的:
字节[] =大海捞针新的字节[] {1,2,3,4,5,1,2,3};
字节[] =针新的字节[] {1,2,3};
INT指数= 0;
,而(真)
{
指数= SearchBytes(草堆,针,指数);
如果(指数== -1)
中断;
Console.WriteLine(+指数在找到);
指数+ = needle.Length;
}
这循环开始于指数0,则使用以前的搜索结果设置一个新的指数开始下一次搜索。
它增加了 needle.Length
来索引,使我们。开始之前找到的结果结束后,立即搜索
更新:
下面是这个代码是如何可以被用来创建一个返回索引作为数组的方法:
公众诠释[] SearchBytesMultiple (字节[]草垛,字节[]针)
{
INT指数= 0;
名单,LT; INT>结果=新的List< INT>();
,而(真)
{
指数= SearchBytes(草堆,针,指数);
如果(指数== -1)
中断;
results.Add(指数);
指数+ = needle.Length;
}
返回results.ToArray();
:
}
就可以这样使用
和
字节[] =大海捞针新的字节[] {1,2,3,4,5,1,2,3};
字节[] =针新的字节[] {1,2,3};
INT [] =指标SearchBytesMultiple(草堆,针);
I'm trying to continuously find a byte array (byte[]) within a byte array and I found a code that only finds the first occurrence.
This is where I found the code: Find an array (byte[]) inside another array?
Question: How can I continuously find a byte array with this code below?
public int SearchBytes(byte[] haystack, byte[] needle)
{
int len = needle.Length;
int limit = haystack.Length - len;
for (int i = 0; i <= limit; i++)
{
int k = 0;
for (; k < len; k++)
{
if (needle[k] != haystack[i + k]) break;
}
if (k == len) return i;
}
return -1;
}
You can change the method to accept a start index like this:
public int SearchBytes(byte[] haystack, byte[] needle, int start_index)
{
int len = needle.Length;
int limit = haystack.Length - len;
for (int i = start_index; i <= limit; i++)
{
int k = 0;
for (; k < len; k++)
{
if (needle[k] != haystack[i + k]) break;
}
if (k == len) return i;
}
return -1;
}
The difference is simply that this method accepts a start_index
and starts the search at this specific index.
Now, you can use it like this:
byte[] haystack = new byte[] { 1, 2, 3, 4, 5, 1, 2, 3 };
byte[] needle = new byte[] {1,2,3};
int index = 0;
while (true)
{
index = SearchBytes(haystack, needle, index);
if (index == -1)
break;
Console.WriteLine("Found at " + index);
index += needle.Length;
}
This loop starts on index 0, then it uses the result of the previous search to set a new index to start the next search.
It adds needle.Length
to the index so that we start searching immediately after the end of the previously found result.
UPDATE:
Here is how this code can be used to create a method that returns the indexes as an array:
public int[] SearchBytesMultiple(byte[] haystack, byte[] needle)
{
int index = 0;
List<int> results = new List<int>();
while (true)
{
index = SearchBytes(haystack, needle, index);
if (index == -1)
break;
results.Add(index);
index += needle.Length;
}
return results.ToArray();
}
And it can be used like this:
byte[] haystack = new byte[] { 1, 2, 3, 4, 5, 1, 2, 3 };
byte[] needle = new byte[] {1,2,3};
int[] indexes = SearchBytesMultiple(haystack, needle);
这篇关于C#中如何不断找到一个字节数组内的字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!