I'm trying to find a byte array byte[] inside another byte array byte[] going reverse.
Question: How can I search a byte array inside another byte array from end to start?
This link is a reference of the code I'm making c# How to Continuously find a byte array inside a byte array?
EDIT: I need this code converted to searching for a byte arry within another byte array going reverse.
indexPos = SearchBytes(input, find, indexPos);
Console.WriteLine("Found at " + indexPos);
indexPos += find.Length;
UPDATED: When I click the button, the index needs to search as follows: 11, 6 1
This code below is what I need to have searching from end to start:
byte[] input = { 0, 1, 1, 1, 5, 6, 1, 1, 1, 7, 8, 1, 1, 1 };
byte[] find = { 1, 1, 1 };
int indexPos = 0;
private void button1_Click(object sender, EventArgs e)
{
indexPos = SearchBytes(input, find, indexPos);
Console.WriteLine("Found at " + indexPos);
indexPos += find.Length;
}
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;
}
haystack0, 1, 1, 1, 1, 5, 6, 7, 8and yourneedleis1would you return1or4as index position? And if yourneedleis1 5, would you return index4,5, or would you return-1(not found)? And lastly, if yourneedleis5 1, would you return4,5, or would you return-1(not found)?haystack1 2 1 2 1 2? if your needle is1 2, would you return 4 then 2 then 0 (note that I bold it)? or, you would read from backwards2 1 2 1 2 1and thus the last one will lack of2and you will only return 4 then 2?haystack0 1 0 0 1 1 0needle 1I need it to find the numbers in this order: 5, 4, 1. Then the next index will be -1 to restart. I'm simply trying to make the code reverse its searching.. I'll appreciate an answer. Thanks.byte[]in yourneedleinstead ofbyteif you only can have 1byte. That's why I keep giving examples of multiple bytes asneedle. So, will yourneedleonly be 1byte? can it be multiple bytes?