Advertisement
kilya

Change Hex value

Feb 6th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Diagnostics;
  7.  
  8. namespace ChangeValue
  9. {
  10.     public partial class FrmMain : Form
  11.     {
  12.         public FrmMain()
  13.         {
  14.             InitializeComponent();
  15.         }
  16.         OpenFileDialog ofd = new OpenFileDialog();
  17.  
  18.         private void BtnLoad1_Click(object sender, EventArgs e)
  19.         {
  20.             ofd.Filter = "Videos files (*.png *.jpg) | *.png; *.jpg; | All files (*.*) | *.*";
  21.             DialogResult result = ofd.ShowDialog();
  22.             if (result == DialogResult.OK)
  23.             {
  24.                 path1.Text = ofd.FileName;
  25.             }
  26.         }
  27.  
  28.         private void BtnLoad2_Click(object sender, EventArgs e)
  29.         {
  30.             ofd.Filter = "Videos files (*.png *.jpg) | *.png; *.jpg; | All files (*.*) | *.*";
  31.             DialogResult result = ofd.ShowDialog();
  32.             if (result == DialogResult.OK)
  33.             {
  34.                 path2.Text = ofd.FileName;
  35.                 BtnStart.Enabled = true;
  36.             }
  37.         }
  38.  
  39.         private void BtnStart_Click(object sender, EventArgs e)
  40.         {
  41.             byte[] image1 = File.ReadAllBytes(path1.Text);
  42.             string hex1 = Bytes2HexString(image1);
  43.             // search the value from hex1
  44.             string copyFromhex1 = "";
  45.             string pattern = @"(?<=\w{60})\w{4}";
  46.             Match match1 = Regex.Match(hex1, pattern);
  47.             if (match1.Success)
  48.             {
  49.                 copyFromhex1 = match1.Value;
  50.             }
  51.  
  52.             byte[] image2 = File.ReadAllBytes(path2.Text);
  53.             string hex2 = Bytes2HexString(image2);
  54.             // search the value from hex2
  55.             pattern = @"(?<=\w{60})\w{4}";
  56.             Match match2 = Regex.Match(hex1, pattern);
  57.             if (match2.Success)
  58.             {
  59.                 hex2.Replace(match2.Value,copyFromhex1);
  60.             }
  61.  
  62.             image2 = HexString2Bytes(hex2);
  63.             File.WriteAllBytes(path2.Text, image2);
  64.             //Process.Start(path2.Text);
  65.             MessageBox.Show("Success");
  66.         }
  67.  
  68.         private static byte[] HexString2Bytes(string hexString)
  69.         {
  70.             int bytesCount = (hexString.Length) / 2;
  71.             byte[] bytes = new byte[bytesCount];
  72.             for (int x = 0; x < bytesCount; ++x)
  73.             {
  74.                 bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
  75.             }
  76.  
  77.             return bytes;
  78.         }
  79.  
  80.         private static string Bytes2HexString(byte[] buffer)
  81.         {
  82.             var hex = new StringBuilder(buffer.Length * 2);
  83.             foreach (byte b in buffer)
  84.             {
  85.                 hex.AppendFormat("{0:x2}", b);
  86.             }
  87.             return hex.ToString();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement