using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace HexToFileConverter { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void textBox1_TextChanged(object sender, EventArgs e) { } private void btnWriteToFile_Click(object sender, EventArgs e) { string fileHex = txtHexCode.Text; // Should refactor this to extract message box to single method. if (cmbFileType.Text == "Document") { string dialogueMessage = WriteDocumentToFile(fileHex); MessageBox.Show(dialogueMessage); } if (cmbFileType.Text == "Message") { string dialogueMessage = WriteMessageToFile(fileHex); MessageBox.Show(dialogueMessage); } if (cmbFileType.Text == "PDF") { string dialogueMessage = WriteHexToPDF(fileHex); MessageBox.Show(dialogueMessage); } if (cmbFileType.Text == "Image") { string dialogueMessage = WriteHexToJPG(fileHex); MessageBox.Show(dialogueMessage); } if (cmbFileType.Text != "Message" && cmbFileType.Text != "Document" && cmbFileType.Text != "PDF" && cmbFileType.Text != "Image") { MessageBox.Show("Please select a file type."); } } static string WriteDocumentToFile(string fileHex) { string fileName = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); byte[] fileBytes = HexStringToBytes(fileHex); // Write to .doc file File.WriteAllBytes(@"C:\\Users\\Public\\Documents\" + fileName + ".doc", fileBytes); return "File saved as " + fileName + ".doc"; } static string WriteMessageToFile(string fileHex) { string fileName = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); byte[] fileBytes = HexStringToBytes(fileHex); // Write to .doc file File.WriteAllBytes(@"C:\\Users\\Public\\Documents\" + fileName + ".msg", fileBytes); return "File saved as " + fileName + ".msg"; } static string WriteHexToPDF(string fileHex) { string fileName = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); // Remove "0x" prefix if present if (fileHex.StartsWith("0x")) fileHex = fileHex.Substring(2); // Convert hex to bytes byte[] pdfBytes = HexStringToPDF(fileHex); File.WriteAllBytes(@"C:\\Users\\Public\\Documents\" + fileName + ".pdf", pdfBytes); return "File saved as " + fileName + ".pdf"; } static string WriteHexToJPG(string fileHex) { string fileName = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); byte[] fileBytes = HexStringToBytes(fileHex); // Write to .doc file File.WriteAllBytes(@"C:\\Users\\Public\\Documents\" + fileName + ".jpg", fileBytes); return "File saved as " + fileName + ".jpg"; } static byte[] HexStringToBytes(string hex) { if (string.IsNullOrWhiteSpace(hex)) throw new ArgumentException("Hex string is null or empty."); // Remove 0x prefix if present if (hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) hex = hex.Substring(2); // Remove whitespace, newlines, etc. hex = hex.Replace(" ", "").Replace("\n", "").Replace("\r", ""); // If odd length, pad with a '0' at the end if (hex.Length % 2 != 0) { Console.WriteLine("Warning: Odd-length hex string detected. Padding with 0."); hex += "0"; } int length = hex.Length; byte[] result = new byte[length / 2]; for (int i = 0; i < length; i += 2) { result[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); } return result; } static byte[] HexStringToPDF(string hex) { if (hex.StartsWith("<")) hex = hex.Substring(1); if (hex.EndsWith(">")) hex = hex.Substring(0, hex.Length - 1); byte[] bytes = new byte[hex.Length / 2]; for (int i = 0; i < bytes.Length; i++) bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); return bytes; } static byte[] HexStringToByteArray(string hex) { byte[] data = new byte[hex.Length / 2]; for (int i = 0; i < data.Length; i++) { data[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16); } return data; } public static string WriteToFile() { return "Success"; } private void frmMain_Load(object sender, EventArgs e) { } } }