Home > DeveloperSection > Forums > Binary Pattern Search
Andrew Watson
Andrew Watson

Total Post:39

Posted on    April-23-2011 4:25 AM

 C# C# 

 1 Reply(s)
 1857  View(s)
Rate this:
Hi Everyone,

I wanna search a binary file for this pattern '00 00 00 08 00' then to read 8 bytes after it into a variable.

Please help!

Uttam Misra

Total Post:125

Posted on    April-23-2011 4:30 AM

namespace XYZ
    public partial class Form1 : Form {

        StringBuilder bldr = new StringBuilder();
        byte[] readFromFile;

        Regex hexPattern = new Regex("0000000800(?<binaryText>.{16})");

        MatchCollection matchBinary;

        FileStream bStream = new FileStream("C:\\XYZ.bin", FileMode.Open, FileAccess.Read);
        BinaryReader fruity;

        public Form1() {

            using (fruity = new BinaryReader(bStream)) {
                readFromFile = fruity.ReadBytes(31);

            // create a hex-dump formatted string
            // from the binary file input data
            foreach (byte thisbyte in readFromFile) {

            // then search the hex-dump-format string for the
            // pattern he seeks. 'Capture' the eight bytes which
            // follow the pattern.
            matchBinary = hexPattern.Matches(bldr.ToString());

            // No recovery required ... just index the data bytes
            // in 'readFromFile' using the index supplied by Regex.
        }// end constructor

    }// end class Form1

}// end Namespace ‘XYZ’

Don't want to miss updates? Please click the below button!

Follow MindStick