BINARY PATTERN SEARCH

Andrew Watson

Total Post:39

Points:273
Posted by  Andrew Watson
C# 
C#
 2056  View(s)
Ratings:
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!
  1. Uttam Misra

    Post:127

    Points:915
    Re: Binary Pattern Search

    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() {
                InitializeComponent();
                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) {
                    bldr.Append(thisbyte.ToString("x2"));
                }
                // 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’

      Modified On Mar-29-2018 04:06:56 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!