Solved

RegEx Search a Byte Array

Posted on 2011-09-06
3
1,322 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 74

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

746 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

11 Experts available now in Live!

Get 1:1 Help Now