Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to search array of byte[] for sub-array

Posted on 2006-06-04
5
Medium Priority
?
328 Views
Last Modified: 2010-04-16
I am reading a binary file into a one dimensional array of byte[]. Now what I'd like to do is search this array for a sub-array of byte[]. In other words, this would be the equivilent of String.IndexOf(). For some reason I just can't seem to wrap my head around this. (Performance is not a huge issue for my application. ) If anyone could post an example of how to accomplish this, it would be most appreciated.

Here is what I tried, but it always returns 0.


        private int IndexOf(byte[] searchIn, byte[] searchFor)
        {
            for (int i = 0; i < searchIn.Length; i++)
            {
                bool match = true;
                for (int j = 0; j < searchFor.Length; j++)
                {
                    int test = searchIn[i];
                    if (searchIn[i] != searchFor[j])
                    {
                        match = false;
                        //continue;
                    }
                }
                if (match) return i;
            }
            return 0;
        }
0
Comment
Question by:toddhd
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:aaronfeng
ID: 16829158
I'm a little bit lost on what you are trying to do, maybe it's just me.  For example,

byte[] searchIn = new byte[] { 1, 1, 2, 2, 3, 3 };
byte[] searchFor = new byte[] { 1, 4, 4, 5, 5 };

This would return 0.  The only time it would return the index is when searchFor[i] is equals to every byte in searchFor.  Is this what you want?  Looks weird.

If you can give some examples, how you want to call your IndexOf method and provide some input would be helpful.

Cheers,

Aaron
http://aaronfeng.blogspot.com
0
 
LVL 8

Author Comment

by:toddhd
ID: 16829197
Here is what I'm doing from an application level. I have a binary file from a 3rd party application. I don't really know the internal structure of the file enough to replicate and load it in my program natively. I know enough to hack it with a hex editor however. For example, I can open the file in a hex editor and search for "FirstName" (in text, I don't know the value in hex off the top of my head), and I know that the person's first name follows and continues until I reach a a hex 00 00 inidicating the end.

Going in and hacking the file by hand is a pain. I want to automate the process.

So I read the binary file into a byte array in memory. Now I need to search that file for "FirstName". The first thing I do is convert "FirstName" to hex, which is again, a one dimensional byte array.

Now I just need to "search" the binary file for this particular hex pattern, and return the index of where the string was located.

If we were talking about strings instead of bytes, the String.IndexOf() is what I'm trying to do.

So if my binary file looked like this in hex:

01 04 A9 10 05 FF 3D 50

I might search for this:

A9 10 05

Then it shoudl return an index of 2.

Does that make more sense?



0
 
LVL 5

Accepted Solution

by:
aaronfeng earned 500 total points
ID: 16829281
Try this:

private int IndexOf2(byte[] searchIn, byte[] searchFor) {
  if (searchFor.Length > searchIn.Length) return 0;
  for (int i = 0; i < searchIn.Length; i++) {
    int startIndex = i;
    bool match = true;
    for (int j = 0; j < searchFor.Length; j++) {
      if (searchIn[startIndex] != searchFor[j]) {
        match = false;
        break;
      }
      else if (startIndex < searchIn.Length) {
        startIndex++;
      }

        if (match) return startIndex;
      }
  }
  return 0;
}


Cheers,

Aaron
http://aaronfeng.blogspot.com
0
 
LVL 8

Author Comment

by:toddhd
ID: 16829550
Thanks Aaron. That wasn't quite correct, but it did set me on the right path. The problem with the code above is that if the first character matches, then it returns true without matching the rest of the characters. Boggled my mind, because it was working sometimes and not others. Also, when it did work, it was always off by one (because startIndex++ kicked off before returning).  Anyway, I tweaked it a bit, sort of a hack really, but this works:

        private int IndexOf(byte[] searchIn, byte[] searchFor)
        {
            if ((searchIn != null) && (searchIn != null))
            {
                if (searchFor.Length > searchIn.Length) return 0;
                for (int i = 0; i < searchIn.Length; i++)
                {
                    int startIndex = i;
                    bool match = true;
                    for (int j = 0; j < searchFor.Length; j++)
                    {
                        if (searchIn[startIndex] != searchFor[j])
                        {
                            match = false;
                            break;
                        }
                        else if (startIndex < searchIn.Length)
                        {
                            startIndex++;
                        }

                    }
                        if (match)
                            return startIndex - searchFor.Length;
                }
            }
            return 0;
        }
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 16829568
Yes, you are correct.  I did the code really fast, but I'm glad you found the mistake.

Cheers,

Aaron
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question