find byte array inside another byte array

Hi. I have a large binary array and need to find all positions of two specific bytes in this array. For example I need to know where is in the array starts AA AA, and then another position of AA AA  until end of the array. I don't  need to search using for next it will take a lot of time and I need to think about speed.
LVL 1
mastiSoftAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Change the find variable and query lines as follows to this if what you have is a Byte array and you what to find which nibble has the find value in it.
byte find = 0xe;

var results = TestArray.Select((item, index) =>
    ((((item >> 4) & 0x0f) == find) || ((item & 0x0f) == find)) 
    ? index 
    : -1)
    .Where(i => i > -1).ToList();

Open in new window

0
 
Chinmay PatelEnterprise ArchitectCommented:
Hi mastiSoft,

When you say large - can you please define approximate size and expected time to finish the search?

Regards,
Chinmay.
0
 
mastiSoftAuthor Commented:
it is about 3600 bytes , because data is 16 bit I need to find two bytes, I saying AAAA but it can be another combination. I expect that this will be repeating after each (let say 400 bytes) but I cannot be sure data can be corrupted .
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Fernando SotoRetiredCommented:
Hi mastiSoft;

The code snippet should do what you need.
// The variable bytes is the large byte array
// results is a List<int> holding the index of within bytes of the byte you want to find
// find holds the byte you are looking for in this case hex AA
byte find = 0xaa;
var results = bytes.Select((item, index) => item == find ? index : -1).Where(i => i > -1).ToList();

Open in new window

0
 
mastiSoftAuthor Commented:
Hi Fernando.
Maybe I didn't understood solution but when I  made a test program:
   private void button1_Click(object sender, EventArgs e)
        {
            TestArray = new byte[11];
            TestArray[0] = 0xAA;
            TestArray[1] = 0x01;
            TestArray[2] = 0x05;
            TestArray[3] = 0xEE;
            TestArray[4] = 0xEE;
            TestArray[5] = 0xC0;
            TestArray[6] = 0xFA;
            TestArray[7] = 0x0C;
            TestArray[8] = 0xEE;
            TestArray[9] = 0xEE;
            TestArray[10] = 0x01;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            List<int> list = new List<int>();
            byte find = 0xee;
            var results = TestArray.Select((item, index) => item == find ? index : -1).Where(i => i > -1).ToList();

            int[] Res;
            Res = list.ToArray();
            for (int j = 0; j< Res.Length; j++)
            {
                listBox1.Items.Add(Res[j].ToString());
            }
        }
I found that I don't have anything in list.
0
 
Fernando SotoRetiredCommented:
Hi mastiSoft;

The issue you are having is that you using an empty List<> object to fill the array Res at this line
int[] Res;
Res = list.ToArray();

Open in new window

Change to using the results of the query and it will work.
int[] Res;
Res = results.ToArray();

Open in new window

0
 
mastiSoftAuthor Commented:
now it works, but the algorithm find all positions of the for example 0xee as in included code.
when I changed the array to
   TestArray = new byte[11];
            TestArray[0] = 0xAA;
            TestArray[1] = 0x01;
            TestArray[2] = 0x05;
            TestArray[3] = 0xEE;
            TestArray[4] = 0xEE;
            TestArray[5] = 0xC0;
            TestArray[6] = 0xFA;
            TestArray[7] = 0x0C;
            TestArray[8] = 0xEE;
            TestArray[9] = 0xAE;
            TestArray[10] = 0x01;

I received position [8] as well but on position [9] I have 0xAE.
Is it the only way to find some array inside another array?
0
 
Fernando SotoRetiredCommented:
To your statement
I received position [8] as well but on position [9] I have 0xAE.
Is it the only way to find some array inside another array?
It sounds like you want to find all occurrences of the hex value of 0xE in each byte? A single byte has 8 bits of data which has two hex digits such as 0xEE or in binary 1110 1110. When you talk about finding, "some array inside another array", that is not what you have defined here. So I guess I need to get a better grab of what you need. Your input data how is it defined? What do you want to do? by the way 8 bits in a data structure is called a byte and 4 bits in a data structure is called a nibble.
0
 
mastiSoftAuthor Commented:
I have a data flow from device, in this data flow I have a few traces. I have to catch up the all those traces and they starts with AA AA  . That's why I need to find two bytes , I can get a hundred results with just AA in the data flow and this will slow down the process.
I tested your code and it still find the position 8 .
0
 
Fernando SotoRetiredCommented:
What do yo want to find in the byte array?
0
 
mastiSoftAuthor Commented:
in the byte array I want to find another array. For example I need to find where in the large array starts AA AA 0C EE . Right now I find AA then check next position if it is also AA then next if it is 0C and so on. I think this is an ancient technology .
0
 
Fernando SotoRetiredCommented:
To your statement, "in the byte array I want to find another array.", the Byte array data structure holds objects of type byte and NOT arrays of Bytes so your understanding of it is not correct. If you need to find a pattern of consecutive bytes you will need to find the start of a possible pattern and then check the others to see if they match as well. The code snippet below does that and enters the start of the patterns found within TestArray into the ListBox.
// The byte pattern to find within the byte array TestArray
byte[] findPattern = { 0xAA, 0xAA, 0x0C, 0xEE };
// The length of the pattern to find
int patternLength = findPattern.Length;

// Check the first position of the pattern to the current position in TestArray
for (int i = 0; i < TestArray.Length - patternLength; i++)
{
    // If you get here then check the other posittions in pattern to the next positions in the TestArray
    if (TestArray[i] == findPattern[0])
    {
        bool found = true;
        for (int j = 1; j < patternLength; j++)
        {
            if (TestArray[i+j] != findPattern[j])
            {
                // Pattern was not found go to outer loop and check next
                found = false;
                break;
            }
        }
        // If the pattern was found add it to the ListBox
        if (found) listBox1.Items.Add(i.ToString());
    }
}

Open in new window

0
 
mastiSoftAuthor Commented:
Hi Fernando.  That is how I did all this years using another language and I thought it was a better solution in C#.  It's seams I was wrong and have to use the same algorithm as before.
Thank you for your patience and time.
0
 
Fernando SotoRetiredCommented:
Not a problem mastiSoft, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.