troubleshooting Question

Finding a pattern of bytes within an Array of bytes.

Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America asked on
.NET ProgrammingC#
5 Comments1 Solution799 ViewsLast Modified:
Hi, I'm using MS VS2010, C#.NET. I have an Array of bytes wherein I need to identify a pattern of multiple bytes. Much like the IndexOf() method does, but with an array of bytes.

Therefore, if my array contained "0A0A0A0A" (which is four tens in a row), I would like to know the index of where that pattern begins.

I came up with a way to find a NULL value, but it doesn't work for patterns of multiple bytes. Can someone provide some help to get me back on track?

Obviously I can convert the bytes to string and then look for the string equivalent, but there has to be a way to do it without so many conversions, which can cause "misreads".


// hexAddress - the starting address of the string
// bytes2ReadAsString - the amount of bytes to read past the starting point.

private string findNull(string hexAddress, string bytes2ReadAsString)
            int counter = 0;
            string i = "";
            br.BaseStream.Position = (Int32.Parse(hexAddress, System.Globalization.NumberStyles.HexNumber));
            foreach (byte mybyte in br.ReadBytes(Convert.ToInt32(bytes2ReadAsString)))
                if (mybyte.Equals(0x0))
            i = counter.ToString("X");

            // This returns the index of where the NULL begins.
            return i;

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros