Solved

RegEx Search a Byte Array

Posted on 2011-09-06
3
1,333 Views
Last Modified: 2013-12-17
I need to be able to search through a byte array with a string pattern (all numeric values greater than six digits) and return that value to the calling application. I know how to search for a specific string in an array but can't make the jump to a regex pattern.
0
Comment
Question by:missioni
  • 2
3 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36490813
I'm not entirely sure what you have. Typically, a byte array is declared like so:

byte[] temp = new byte[4096];

What you seem to be describing is more like this:

List<byte[]> temp = new List<byte[]>();

If you are using the typical byte[] when you say "byte array" then you can't search it with a string pattern the way you are implying. You would have to search sections of the whole array like so:

for (int i = 0; i< myByteArr.Length; i++)
{
      if(myBytArr[i].ToString() + myBytArr[i + 1].ToString() ... myBytArr[i + byteRepresentation.Length] = byteRepresentation)
       {
            return true;
        }
}

Open in new window

If you want to do a regex, then you will need a string representation of the byte[].
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36490818
Something like this?
byte[] source = new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 49, 50, 51, 52, 53, 54, 55 };
string converted = System.Text.Encoding.ASCII.GetString(source);
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(converted, @"\d{6,}");

if (m.Success)
{
    Console.WriteLine(m.Value);
}

Console.ReadKey();

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 36490889
And you could always skip the regex part and just look for a series of numerics:
byte[] source = new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 49, 50, 51, 52, 53, 54, 55 };
List<byte> matched = new List<byte>();

for (int i = 0; i < source.Length; i++)
{
    if (source[i] >= 48 && source[i] <= 57)
    {
        matched.Add(source[i]);
    }
    else
    {
        if (matched.Count <= 6)
        {
            matched.Clear();
        }
        else
        {
            break;
        }
    }
}

// return matched.ToArray()

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now