Solved

RegEx Search a Byte Array

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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