5

Possible Duplicate:
byte[] array pattern search

Let's say I have an array of bytes:

byte[] myArray = new byte[]{1,2,3,4,5,6,7,1,9,3,4,3,4,7,6,5,6,7,8};

how can I determine if myArray contains bytes 9,3,4,3 in that order? do I have to iterate through the array appending each element to a string then use the String.Contains() method to know if that byte array contains those elements in that order?

I know I can do semething like:

String s = "";
foreach(byte b in myArray)
{
  s = s + b.ToString();
}

//then do  

s.Contains("9343")

this is not to efficient on long arrays. What will be a more efficient way of doing this?

Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • if your are thinking of making a string to do a Contains: string myString = System.Text.Encoding.ASCII.GetString(myByteArray ) but wait for a proper answer ;-) –  Sep 25 '11 at 16:41
  • I don't need to convert it to a string. I just used a string because that is the only method that I know of that will enable me to do that type of comparison. that was useful though thanks a lot – Tono Nam Sep 25 '11 at 16:43
  • true there is a similar question sorry I did not find it. – Tono Nam Sep 25 '11 at 16:44

2 Answers2

9

Try the following

public static bool ContainsSequence(byte[] toSearch, byte[] toFind) {
  for (var i = 0; i + toFind.Length < toSearch.Length; i++) {
    var allSame = true;
    for (var j = 0; j < toFind.Length; j++) {
      if (toSearch[i + j] != toFind[j]) {
        allSame = false;
        break;
      }
    }

    if (allSame) {
      return true;
    }
  }

  return false;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • In my quick testing, this had an off by one error. If the data to find was the last elements in the array, it exited the loop early. I will do a bit more testing to verify this and post the answer, but for now it's basically chanting the first loop condition to `<=` – CubanX Dec 06 '16 at 13:00
  • 1
    this needs to be changed to "for (var i = 0; i + toFind.Length <= toSearch.Length; i++) {" to work if "toFind" is last in "toSearch" – nietras Aug 22 '18 at 12:01
1

The simplest algorithm that works and is to rip through the byte array until you find a match on the first byte in the byte pattern that you're looking for then walk along through the two until you reach the end, or if you find a mismatch, continue from where you left off. This can "degrade" if you keep getting partial matches. Depending on your needs, this could be good enough (it's simple to write, simple to maintain).

If that isn't fast enough for your purposes, you can easily adopt Boyer-Moore.

jason
  • 236,483
  • 35
  • 423
  • 525