Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

RegEx Search a Byte Array

Posted on 2011-09-06
3
Medium Priority
?
1,485 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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