Advertisement
Blizzardo1

Hex Compare Test

Jan 21st, 2019
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9.  
  10. namespace HexComparer
  11. {
  12.     internal class HexCompare : Form {
  13.         private long _yline, _ydiff;
  14.         private Label _label3;
  15.         private TextBox _textBox1, _textBox2;
  16.         private DataGridView _dataGridView;
  17.         private Button _btnDisplay;
  18.  
  19.         private Point PointTo( int x, int y ) => new Point( x, y );
  20.         private Size SizeTo( int w, int h ) => new Size( w, h );
  21.  
  22.         public HexCompare( ) {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private void InitializeComponent( ) {
  27.             SuspendLayout( );
  28.             _label3 = new Label {
  29.                 AutoSize = true,
  30.                 Location = PointTo( 9, 12 ),
  31.                 Name = "_label3"
  32.             };
  33.  
  34.             _textBox1 = new TextBox {
  35.                 Size = SizeTo( 170, 23 ),
  36.                 Location = PointTo( 9, 24 ),
  37.                 Name = "_textBox1"
  38.             };
  39.  
  40.             _textBox2 = new TextBox {
  41.                 Size = SizeTo( 170, 23 ),
  42.                 Name = "_textBox2"
  43.             };
  44.             _textBox2.Location = PointTo( _textBox1.Location.X + _textBox2.Size.Width + 16, _textBox1.Location.Y );
  45.  
  46.             _dataGridView = new DataGridView {
  47.                 Size = SizeTo( 350, 200 ),
  48.                 Location = PointTo( 9, _textBox1.Location.Y + 24 ),
  49.                 Name = "_dataGridView"
  50.             };
  51.  
  52.             _dataGridView.Columns.Add( "Compare", "Compare" );
  53.  
  54.             _btnDisplay = new Button {
  55.                 Size = SizeTo( 100, 23 ),
  56.                 Name = "_btnDisplay",
  57.                 Text = "Check Files"
  58.             };
  59.             _btnDisplay.Location = PointTo( _dataGridView.Width - _btnDisplay.Size.Width + 8,
  60.                 _dataGridView.Height + _btnDisplay.Size.Height + 32 );
  61.             _btnDisplay.Click += btnDisplay_Click;
  62.            
  63.             Name = "HexCompare";
  64.             Text = "Hex Comparer";
  65.             Size = SizeTo(400, 450 );
  66.  
  67.             Controls.AddRange( new Control[] {_label3, _textBox1, _textBox2, _dataGridView, _btnDisplay} );
  68.  
  69.             ResumeLayout( );
  70.         }
  71.  
  72.         private void btnDisplay_Click(object sender, EventArgs e)
  73.         {
  74.             if (!((_textBox1.Text == "") | (_textBox2.Text == "")))
  75.             {
  76.                 _label3.Text = "";
  77.                 _dataGridView.Rows.Clear();
  78.                 Cursor = Cursors.WaitCursor;
  79.                 FileEquals(_textBox1.Text, _textBox2.Text);
  80.                 Cursor = Cursors.Default;
  81.                 _label3.Text = string.Concat(_ydiff, " different line(s) found");
  82.                 MessageBox.Show("Done !");
  83.             }
  84.             else
  85.             {
  86.                 MessageBox.Show("Error: Select Two Files For Comparison");
  87.             }
  88.         }
  89.  
  90.  
  91.         private bool FileEquals(string file1, string file2) {
  92.             bool flag;
  93.             using (var fileStream = new FileStream(file1, FileMode.Open, FileAccess.Read, FileShare.Read))
  94.             {
  95.                 using (var fileStream1 = new FileStream(file2, FileMode.Open, FileAccess.Read, FileShare.Read))
  96.                 {
  97.                     using (var binaryReader = new BinaryReader(fileStream))
  98.                     {
  99.                         using (var binaryReader1 = new BinaryReader(fileStream1))
  100.                         {
  101.                             _yline = 0;
  102.                             _ydiff = 0;
  103.                             while (true)
  104.                             {
  105.                                 _yline++;
  106.                                 byte[] numArray = binaryReader.ReadBytes(16);
  107.                                 byte[] numArray1 = binaryReader1.ReadBytes(16);
  108.                                 const int num = 16;
  109.                                 string str = numArray.Select(( c, i ) => new { Char = c, Chunk = i / num }).GroupBy((c) => c.Chunk).Select((g) => (
  110.                                     from c in g
  111.                                     select $"{c.Char:X2} " ).Aggregate(string.Concat)).Select(( s, i ) =>
  112.                                     $"{_yline * num:d6}: {s}" ).Aggregate("", ( s, i ) => string.Concat(s, i, Environment.NewLine));
  113.                                 string str1 = numArray1.Select(( c, i ) => new { Char = c, Chunk = i / num }).GroupBy((c) => c.Chunk).Select((g) => (
  114.                                     from c in g
  115.                                     select $"{c.Char:X2} " ).Aggregate(string.Concat)).Select(( s, i ) =>
  116.                                     $"{_yline * num:d6}: {s}" ).Aggregate("", ( s, i ) => string.Concat(s, i, Environment.NewLine));
  117.                                 if (numArray.Length != numArray1.Length)
  118.                                 {
  119.                                     _dataGridView.Rows.Add( _yline, str, str1, "", "" );
  120.                                     _ydiff++;
  121.                                 }
  122.                                 if (numArray.Length == 0)
  123.                                 {
  124.                                     break;
  125.                                 }
  126.  
  127.                                 if ( numArray.SequenceEqual( numArray1 ) ) continue;
  128.  
  129.                                 _dataGridView.Rows.Add( _yline, str, str1, "", "" );
  130.                                 _ydiff++;
  131.                             }
  132.                             flag = true;
  133.                         }
  134.                     }
  135.                 }
  136.             }
  137.             return flag;
  138.         }
  139.  
  140.         private string Offset(byte[] ba)
  141.         {
  142.             var stringBuilder = new StringBuilder(ba.Length * 2);
  143.             byte[] numArray = ba;
  144.             foreach ( byte t in numArray ) {
  145.                 stringBuilder.Append($"{t:x2}");
  146.             }
  147.             return stringBuilder.ToString();
  148.         }
  149.  
  150.         private string YtoString(byte[] b) => b.Aggregate( "", ( current, t ) => string.Concat( current, " ", t.ToString( ) ) );
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement