读一个十六进制的bin文件,在bin文件添加四行头,生成新的bin文件。bin文件可以用vs打开查看。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ACU_004GEN { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string vendorCode = ""; private string deviceType = ""; private string hardwareVersion = ""; private string crc32 = ""; private string firmwareLength = ""; private string sourceBinFile = ""; private string saveBinFile = ""; private void Form1_Load(object sender, EventArgs e) { buttonGenerate.Enabled = false; lblMsg.Text = ""; lblMsg.ForeColor = Color.Empty; } private void label1_Click(object sender, EventArgs e) { } private void textBox5_TextChanged(object sender, EventArgs e) { } private void labelCheckSum_Click(object sender, EventArgs e) { } private void buttonImportBinary_Click(object sender, EventArgs e) { lblMsg.Text = ""; lblMsg.ForeColor = Color.Empty; vendorCode = RightAddZero(textBoxVendorCode.Text.Trim(), 2); deviceType = LeftAddZero(textBoxDeviceType.Text.Trim(), 18); hardwareVersion = RightAddZero(textBoxHardwareVersion.Text.Trim(), 4); crc32 = RightAddZero("CRC3", 4); OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*"; dialog.Title = string.Format("Open Firmware File"); dialog.FilterIndex = 0; dialog.RestoreDirectory = true; if (dialog.ShowDialog() != DialogResult.Yes) { sourceBinFile = dialog.FileName; textBoxBinaryPath.Text = sourceBinFile; try { if (!File.Exists(sourceBinFile)) { return; } buttonGenerate.Enabled = true; } catch (System.Exception ex) { MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private void ReadWirteBinFile() { byte[] binContent = File.ReadAllBytes(sourceBinFile); int binLength = binContent.Length; firmwareLength = RightAddZero(binContent.Length.ToString(),4); //byte[] buffer = System.Text.Encoding.ASCII.GetBytes("NTABCD"); using (FileStream fs = new FileStream(saveBinFile, FileMode.Create, FileAccess.Write)) { string headerRow0 = vendorCode + deviceType.Substring(0, 14); WirteBin(fs, headerRow0, 0); string headerRow1 = deviceType.Substring(13) + hardwareVersion; WirteBin(fs, headerRow1, 1); string headerRow2 = crc32 + firmwareLength; WirteBin(fs, headerRow2, 2); string headerRow3 = "\0"; WirteBin(fs, headerRow3, 3); byte[] binBytes=new byte[16]; int j = 0; int rows = 0; for(int i=0;i< binLength; i++) { binBytes[j] = binContent[i]; j++; //16字节一行 if (j % 16 == 0) { rows++; fs.Position = 16 * (rows+3); fs.Write(binBytes, 0, 16); j = 0; } } } } private void WirteBin(FileStream fs,string str,int row) { byte[] buffer = System.Text.Encoding.ASCII.GetBytes(str); fs.Position = 16 * row; fs.Write(buffer, 0, str.Length); } private void buttonGenerate_Click(object sender, EventArgs e) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*"; saveDlg.Title = string.Format("Save Binary File"); saveDlg.FilterIndex = 0; saveDlg.RestoreDirectory = true; if (saveDlg.ShowDialog() != DialogResult.Yes) { saveBinFile = saveDlg.FileName; textBoxBinaryPath.Text = saveBinFile; try { ReadWirteBinFile(); lblMsg.Text = "It‘s ok!"; lblMsg.ForeColor = Color.LightSeaGreen; buttonGenerate.Enabled = false; } catch (System.Exception ex) { MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } private string RightAddZero(string str, int length) { string retStr = str; for (int i = str.Length;i< length;i++) { retStr = retStr + "\0"; } return retStr; } private string LeftAddZero(string str, int length) { string retStr = str; for (int i = str.Length; i < length; i++) { retStr ="\0"+ retStr; } return retStr; } } }
时间: 2024-10-03 14:41:45