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;
}