Hello,
Peter Ritchie comments are spot on, you need to check each byte.
I just knocked up this :
using System; using System.Collections.Generic; using System.Text;
namespace FindByteArrayinByteArray { class Program { static int indexOf(byte[] array, byte[] value) { int found = 0; for (int i = 0; i < array.Length; i++) { if (array[ i ] == value[found]) { if (++found == value.Length) { return i - found + 1; } } else { found = 0; } } return -1; }
public static void Main() { System.Console.WriteLine(indexOf(new byte[] { 1, 2, 3, 4 }, new byte[] { 1, 2, 3 })); System.Console.WriteLine(indexOf(new byte[] { 1, 2, 3, 4 }, new byte[] { 3, 2, 3 })); System.Console.WriteLine(indexOf(new byte[] { 1, 2, 3, 4 }, new byte[] { 2, 3, 4 })); System.Console.WriteLine(indexOf(new byte[] { 1, 2, 3, 4 }, new byte[] { 4 }));
System.Console.ReadKey(); } } }
Thanks
Steve
|