C# 读写十六进制bin 文件
读一个十六进制的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 FormMain : Form { public FormMain() { InitializeComponent(); } private byte[] binContent; private string vendorCode = ""; private string deviceType = ""; private string hardwareVersion = ""; private byte[] crc32 ; private byte[] checkSum; private byte[] firmwareLength ; private string sourceBinFile = ""; private string saveBinFile = ""; //private uint crc = 0xFFFFFFFF; private uint crc_function = 0xEDB88320; private uint crc32_reverse(byte[] buf) { byte i; uint crc = 0xFFFFFFFF; //uint crc_function = 0xEDB88320; foreach (byte bufByte in buf) { ; i <<= ) { ) { crc >>= ; crc ^= crc_function; } ; ) crc ^= crc_function; } } return (crc); } private uint CheckSum(byte[] buf) { //uint crc = 0xFFFFFFFF; ; //uint crc_function = 0xEDB88320; foreach (byte bufByte in buf) { checkSum = checkSum + bufByte; } return (checkSum); } private void Form1_Load(object sender, EventArgs e) { rbtCheckSum.Checked = true; buttonGenerate.Enabled = false; linkLabelBinaryName.Text = null; } 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) { //openFileDialog1 = new OpenFileDialog(); //OpenFileDialog dialog = new OpenFileDialog(); openFileDialogImport.Filter = "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*"; openFileDialogImport.Title = string.Format("Open Firmware File"); openFileDialogImport.FilterIndex = ; openFileDialogImport.RestoreDirectory = true; if (openFileDialogImport.ShowDialog() != DialogResult.Yes) { //sourceBinFile = openFileDialogImport.FileName; 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() { using (FileStream fs = new FileStream(saveBinFile, FileMode.Create, FileAccess.Write)) { , ); WirteBin(fs, headerRow0, ); ) + hardwareVersion; WirteBin(fs, headerRow1, ); //byte[] headerRow2 = crc32 + firmwareLength; //WirteBin(fs, headerRow2, 2); fs.Position = * ; if (rbtCRC32.Checked == true) { fs.Write(crc32, , ); } else { fs.Write(checkSum, , ); } fs.Position = * + ; fs.Write(firmwareLength, , ); string headerRow3 = "\0"; WirteBin(fs, headerRow3, ); fs.Position = * ; fs.Write(binContent, , binContent.Length); }//end using } private void WirteBin(FileStream fs,string str,int row) { byte[] buffer = System.Text.Encoding.ASCII.GetBytes(str); fs.Position = * row; fs.Write(buffer, , str.Length); } private void buttonGenerate_Click(object sender, EventArgs e) { buttonImportBinary.Enabled = false; vendorCode = LeftAddZero(textBoxVendorCode.Text.Trim(), ); deviceType = LeftAddZero(textBoxDeviceType.Text.Trim(), ); hardwareVersion = LeftAddZero(textBoxHardwareVersion.Text.Trim(), ); SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*"; saveDlg.Title = string.Format("Save Binary File"); saveDlg.FilterIndex = ; saveDlg.RestoreDirectory = true; if (saveDlg.ShowDialog() != DialogResult.Yes) { saveBinFile = saveDlg.FileName; try { ReadWirteBinFile(); buttonGenerate.Enabled = false; } catch (System.Exception ex) { MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); buttonImportBinary.Enabled = true; } } buttonImportBinary.Enabled = true; } 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; } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { sourceBinFile = openFileDialogImport.FileName; linkLabelBinaryName.Text = sourceBinFile; binContent = File.ReadAllBytes(sourceBinFile); firmwareLength = System.BitConverter.GetBytes(binContent.Length); //Array.Reverse(firmwareLength); textBoxFileLength.Text = LeftAddZero(binContent.Length.ToString(); uint crcTmp = crc32_reverse(binContent); crc32 = System.BitConverter.GetBytes(crcTmp); uint checkSumTmp = CheckSum(binContent); checkSum = System.BitConverter.GetBytes(checkSumTmp); //Array.Reverse(crc32); textBoxCRC32.Text = LeftAddZero(crcTmp.ToString(); textBoxCheckSum.Text = LeftAddZero(checkSumTmp.ToString(); } } }
C# 读写十六进制bin 文件的更多相关文章
- 如何将一个二进制的xxx.bin文件轻松转为C语言数组
今天在写一个SPI-flash读写程序,目的是要将一个二进制文件写到SPI_FLASH中,最后通过开机读取,实际上这个.bin文件就是uboot和second-boot的结合体.通过SD卡写到SPI- ...
- 使用Spark读写CSV格式文件(转)
原文链接:使用Spark读写CSV格式文件 CSV格式的文件也称为逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号.在本文中的CSV格 ...
- Keil如何生成bin文件【Keil生成Bin文件的方法】
使用过Keil的同鞋都知道,现在Keil中默认可以输出.axf的调试文件和可以通过钩选输出的.hex可执行文件,没有bin(二进制)文件的输出选项.可是偏偏某些时候需要或者习惯性的使用.bin文件来进 ...
- S03_CH12_基于UDP的QSPI Flash bin文件网络烧写
S03_CH12_基于UDP的QSPI Flash bin文件网络烧写 12.1概述 为了满足不同的需求,本例程在"基于TCP的QSPI Flash bin文件网络烧写"上进行修改 ...
- S03_CH11_基于TCP的QSPI Flash bin文件网络烧写
S03_CH11_基于TCP的QSPI Flash bin文件网络烧写 11.1概述 针对ZYNQ中使用QSPI BOOT的应用,将BOOT.bin文件烧写至QSPI Flash基本都是通过USB C ...
- java 读写properties (配置)文件
Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...
- STM32 Unicode 与 GBK 转换 .bin文件放到SD卡是啥意思
2个数组 : }; }; 一个是Unicode 编码,一个是GBK编码: 用c2b软件转成.bin 二进制文件放到SD卡里: SD卡放入字库 .FON STM32 代码: 代码中SD卡字库和二进制路径 ...
- 处理bin文件
1. fs.Position 写入的位置,从哪个位置开始写 fs.Write(byte1,0,byte1.Length); byte1写入的byte[], 写入内容从第几位开始取,length取多长 ...
- JLINK通过JFLASH烧写bin文件报错处理方法
错误原因:烧写开始地址出错,打开BIN文件后弹出的设置开始地址不正确不能为0 解决措施:用J-FLASH LITE或者将开始地址设置成正确的地址(KEILMDK中IROM1的开始地址
随机推荐
- Ubuntu终端常用的快捷键
Ubuntu终端常用的快捷键 Ubuntu中的许多操作在终端(Terminal)中十分的快捷,记住一些快捷键的操作更得心应手.在Ubuntu中打开终端的快捷键是Ctrl+Alt+T.其他的一些常用的快 ...
- 昨天所写的JQ 点击隐藏事件,关键性原理
JQ 点击隐藏事件,关键性原理 1.JQ 库的调用 一般选择为: 1)库越小越好 2)库的功能越强大越好 <script src="js/jquery.js" type=&q ...
- 请问如何查看mysql 的端口号?
mysql> show variables like 'port'; +---------------+-------+ | Variable_name | Value | +--------- ...
- php中关于 left join 的分页显示
/* * 统计商机搜索总条数信息 * ftt * 2015-11-10 10:13:15 */ public function getBusinessCount($btype='',$buname=' ...
- Spring Quartz定时调度任务配置
applicationContext-quartz.xml定时调度任务启动代码: <?xml version="1.0" encoding="UTF-8" ...
- Eclipse构建Maven项目
1. 安装m2eclipse插件 要用Eclipse构建Maven项目,我们需要先安装meeclipse插件 点击eclipse菜单栏Help->Eclipse Marketpl ...
- 淘宝首页源码藏美女彩蛋(上)(UED新作2013egg)
今日,偶尔翻看淘宝源码,发现竟有美女形状源码.如下图: 此段代码在console中运行,结果更为惊叹. 亲手尝试的读者已经看到了代码运行的结果.taobao.com的console打印出了UED的招聘 ...
- 开始使用 Markdown
(Xee:我最近感觉nyfedit打开有点慢,数据库有点大,试想着用一些其他的方式记录一下学习的过程,才想起了遗忘了很长时间的Markdown,将其分类在HTML下,也是我原本意愿的...) 本文面向 ...
- IOS学习目录
一.UI 1.基础控件 2.高级控件 二.多线程网络 1.网络请求.网络安全 2.
- leetcode 33. Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...