Here is a method that I wrote that you can use to find where the byte[]bytes you are trying to find are in yours .
public static int IndexOfSubarray<T>(this T[] sourceArray, T[] findWhat,
int startIndex, int sourceLength) where T : IEquatable<T>
{
if (sourceArray == null)
throw new ArgumentNullException("sourceArray");
if (findWhat == null)
throw new ArgumentNullException("findWhat");
if (startIndex < 0 || startIndex > sourceArray.Length)
throw new ArgumentOutOfRangeException();
var maxIndex = sourceLength - findWhat.Length;
for (int i = startIndex; i <= maxIndex; i++)
{
if (sourceArray.SubarrayEquals(i, findWhat, 0, findWhat.Length))
return i;
}
return -1;
}
public static bool SubarrayEquals<T>(this T[] sourceArray,
int sourceStartIndex, T[] otherArray, int otherStartIndex, int length)
where T : IEquatable<T>
{
if (sourceArray == null)
throw new ArgumentNullException("sourceArray");
if (otherArray == null)
throw new ArgumentNullException("otherArray");
if (sourceStartIndex < 0 || length < 0 || otherStartIndex < 0 ||
sourceStartIndex + length > sourceArray.Length ||
otherStartIndex + length > otherArray.Length)
throw new ArgumentOutOfRangeException();
for (int i = 0; i < length; i++)
{
if (!sourceArray[sourceStartIndex + i]
.Equals(otherArray[otherStartIndex + i]))
return false;
}
return true;
}
Timwi source
share