-1

I have the following byte arrays. byte[] subArray = { 0x00, 0x01, 0x00, 0x01 }; byte[] array = { 0x1A, 0x65, 0x3E, 0x00, 0x01, 0x00, 0x01, 0x2B, 0x4C, 0xAA };

I would like to identify the subarray and load the preceding bytes into another byte array i.e. result should be as below. byte[] result = {0x2B, 0x4C, 0xAA }

I want to load everything after the subarray.

Any help would be appreciated.

Thanks.

venerik
  • 5,766
  • 2
  • 33
  • 43
user1144852
  • 265
  • 3
  • 6
  • 17
  • 4
    What code have you written so far? What specifically are you having trouble with? – Peter Duniho Oct 31 '14 at 06:36
  • 1
    Even though you are dealing with arrays this might be helpful: https://en.wikipedia.org/wiki/String_searching_algorithm – Enigmativity Oct 31 '14 at 06:39
  • 1
    @Enigmativity Was about to post that link myself. This is the same type of problem as searching for a substring within a string. – Marlon Oct 31 '14 at 06:42

4 Answers4

2

Adapted from my other answer:

byte[] subArray = { 0x00, 0x01, 0x00, 0x01 }; 
byte[] array = { 0x1A, 0x65, 0x3E, 0x00, 0x01, 0x00, 0x01, 0x2B, 0x4C, 0xAA };

var matchIndexes =
    from index in Enumerable.Range(0, array.Length - subArray.Length + 1)
    where array.Skip(index).Take(subArray.Length).SequenceEqual(subArray)
    select (int?)index;

var matchIndex = matchIndexes.FirstOrDefault();
if (matchIndex != null)
{
    byte[] successor = array.Skip(matchIndex.Value + subArray.Length).ToArray();
    // handle successor here
}
Community
  • 1
  • 1
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • 1
    That's why you have 25.6k reputation? Answering no-effort "write me the code" question? – Konrad Kokosa Oct 31 '14 at 06:53
  • 3
    @KonradKokosa: We are here to answer questions, not to instil disciple into the OPs. – Douglas Oct 31 '14 at 06:57
  • 1
    @KonradKokosa The point of SO isn't any particular person - it's building a permanent, easily searched repository of answers on any programming question imaginable. The value gained from anyone being able to look at this and understand how to find a subarray within an array is significantly greater than whatever we'd get from cajoling a non-name user into actually doing their homework. – furkle Oct 31 '14 at 07:02
0

This question describes how to get the first index of the subarray in the parent array. Could easily be modified to get the last index of your sub array by adding the count. From there you can use LINQ Take and Skip or similar array manipulation functions.

Find the first occurrence/starting index of the sub-array in C#

Community
  • 1
  • 1
TysonWolker
  • 436
  • 2
  • 9
0

One way would be to do the below

start from the first position 
 compare corresponding elements of array and sub array
  if there is a match 
    return the indexes as necessary and use the indexes to extract the result sets.
  else
    advance by 1 position
 repeat until you exhaust the whole main array

You can use this method to extract more than one sets of the sub array in the main array.

This is a first cut. There may be other more efficient implementations.

csprabala
  • 177
  • 12
0

Try this. I didn't test this code, there may be some syntax errors, even if doesn't work, try to understand the algorithm.

    byte[] subArray = { 0x00, 0x01, 0x00, 0x01 }; 
    byte[] array = { 0x1A, 0x65, 0x3E, 0x00, 0x01, 0x00, 0x01, 0x2B, 0x4C, 0xAA };
    byte[] result;

    int lastLocation = -1;
    bool control = false;

    foreach(byte x in subArray)
    {
     if(array.contains(x))
     {
      if(location!=-1 || array.IndexOf(x) == lastLocation+1)
      {
       control = true;
       lastLocation = array.IndexOf(x);
      }
      else
      {
       control = false;
       lastLocation = -1;
      }
     }
     else
     {
     control = false;
     lastLocation = -1;
     }
    }
if(control)
{
Array.Copy(array,lastLocation,result,0);
}        
Emre Acar
  • 920
  • 9
  • 24