?
Solved

RegEx Search a Byte Array

Posted on 2011-09-06
3
Medium Priority
?
1,449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 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