using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HexComparer { internal class HexCompare : Form { private long _yline, _ydiff; private Label _label3; private TextBox _textBox1, _textBox2; private DataGridView _dataGridView; private Button _btnDisplay; private Point PointTo( int x, int y ) => new Point( x, y ); private Size SizeTo( int w, int h ) => new Size( w, h ); public HexCompare( ) { InitializeComponent(); } private void InitializeComponent( ) { SuspendLayout( ); _label3 = new Label { AutoSize = true, Location = PointTo( 9, 12 ), Name = "_label3" }; _textBox1 = new TextBox { Size = SizeTo( 170, 23 ), Location = PointTo( 9, 24 ), Name = "_textBox1" }; _textBox2 = new TextBox { Size = SizeTo( 170, 23 ), Name = "_textBox2" }; _textBox2.Location = PointTo( _textBox1.Location.X + _textBox2.Size.Width + 16, _textBox1.Location.Y ); _dataGridView = new DataGridView { Size = SizeTo( 350, 200 ), Location = PointTo( 9, _textBox1.Location.Y + 24 ), Name = "_dataGridView" }; _dataGridView.Columns.Add( "Compare", "Compare" ); _btnDisplay = new Button { Size = SizeTo( 100, 23 ), Name = "_btnDisplay", Text = "Check Files" }; _btnDisplay.Location = PointTo( _dataGridView.Width - _btnDisplay.Size.Width + 8, _dataGridView.Height + _btnDisplay.Size.Height + 32 ); _btnDisplay.Click += btnDisplay_Click; Name = "HexCompare"; Text = "Hex Comparer"; Size = SizeTo(400, 450 ); Controls.AddRange( new Control[] {_label3, _textBox1, _textBox2, _dataGridView, _btnDisplay} ); ResumeLayout( ); } private void btnDisplay_Click(object sender, EventArgs e) { if (!((_textBox1.Text == "") | (_textBox2.Text == ""))) { _label3.Text = ""; _dataGridView.Rows.Clear(); Cursor = Cursors.WaitCursor; FileEquals(_textBox1.Text, _textBox2.Text); Cursor = Cursors.Default; _label3.Text = string.Concat(_ydiff, " different line(s) found"); MessageBox.Show("Done !"); } else { MessageBox.Show("Error: Select Two Files For Comparison"); } } private bool FileEquals(string file1, string file2) { bool flag; using (var fileStream = new FileStream(file1, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var fileStream1 = new FileStream(file2, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var binaryReader = new BinaryReader(fileStream)) { using (var binaryReader1 = new BinaryReader(fileStream1)) { _yline = 0; _ydiff = 0; while (true) { _yline++; byte[] numArray = binaryReader.ReadBytes(16); byte[] numArray1 = binaryReader1.ReadBytes(16); const int num = 16; string str = numArray.Select(( c, i ) => new { Char = c, Chunk = i / num }).GroupBy((c) => c.Chunk).Select((g) => ( from c in g select $"{c.Char:X2} " ).Aggregate(string.Concat)).Select(( s, i ) => $"{_yline * num:d6}: {s}" ).Aggregate("", ( s, i ) => string.Concat(s, i, Environment.NewLine)); string str1 = numArray1.Select(( c, i ) => new { Char = c, Chunk = i / num }).GroupBy((c) => c.Chunk).Select((g) => ( from c in g select $"{c.Char:X2} " ).Aggregate(string.Concat)).Select(( s, i ) => $"{_yline * num:d6}: {s}" ).Aggregate("", ( s, i ) => string.Concat(s, i, Environment.NewLine)); if (numArray.Length != numArray1.Length) { _dataGridView.Rows.Add( _yline, str, str1, "", "" ); _ydiff++; } if (numArray.Length == 0) { break; } if ( numArray.SequenceEqual( numArray1 ) ) continue; _dataGridView.Rows.Add( _yline, str, str1, "", "" ); _ydiff++; } flag = true; } } } } return flag; } private string Offset(byte[] ba) { var stringBuilder = new StringBuilder(ba.Length * 2); byte[] numArray = ba; foreach ( byte t in numArray ) { stringBuilder.Append($"{t:x2}"); } return stringBuilder.ToString(); } private string YtoString(byte[] b) => b.Aggregate( "", ( current, t ) => string.Concat( current, " ", t.ToString( ) ) ); } }