常用的BIN文件操作总结

bin文件,即二进制文件,广泛应用于嵌入式,我们常用的firmware通常会以bin文件或者hex文件格式存储,因此,对bin文件的读写操作其实还是很普遍的,在这里,我记录一下我常用到的bin文件操作。
首先c# winform中有binary文件(bin文件)的基本操作类。 如下所示
filestream file_path = new filestream(文件名, filemode,fileaccess);//binaryreader bin_read = new binaryreader(file_path);binarywriter bin_write = new binarywriter(file_path);如上所示,如果是要读bin文件,那么直接定义binaryreader即可,如果是要写bin文件,定义binarywriter。 读写的基本操作为:
读bin文件的操作为:bin_read. readbyte():返回值为读到的byte值; bin_read. readbytes(count); 返回值为个数为count的byte数组。 还有很多不同返回格式,int,char等,我这里不一一赘述。
写bin文件的操作为:bin_write. write(value):其中value就是要写的值,value可以是byte,int或者char等格式。 bin_write. write(byte[] buffer, int index, int count); 这个方法的含义就是将buffer数组中的一部分值(buffer数组的开始索引为index,长度为count),赋值至bin文件当前位置。
下面我举一个例子,bin文件的写,从0写到255,256个byte。
using system;using system.collections.generic;using system.componentmodel;using system.data;using system.drawing;using system.linq;using system.text;using system.threading.tasks;using system.windows.forms;using system.io;namespace test{ public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { savefiledialog save_file = new savefiledialog(); save_file.filter = bin文件|*.bin; if (save_file.showdialog() == dialogresult.ok) { filestream file_path = new filestream(save_file.filename, filemode.openorcreate,fileaccess.readwrite); binarywriter bin_write = new binarywriter(file_path);//创建bin文件流 byte[] init_byte = new byte[256]; for (int temp = 0; temp < 256; temp++) { init_byte[temp] = (byte)temp; } bin_write.write(init_byte, 0, 256);//给bin文件写内容 bin_write.flush(); bin_write.close(); file_path.close(); } } }}文件运行结果为:
bin文件内容
那么写操作完成了,替换操作要怎么操作呢? 实际中如果要实现hex文件转换为bin文件,那么替换功能将会非常有用,比如将其中的某几个数字改动一下,见代码:
using system;using system.collections.generic;using system.componentmodel;using system.data;using system.drawing;using system.linq;using system.text;using system.threading.tasks;using system.windows.forms;using system.io;namespace test{ public partial class form1 : form { public form1() { initializecomponent(); } private void button1_click(object sender, eventargs e) { savefiledialog save_file = new savefiledialog(); save_file.filter = bin文件|*.bin; if (save_file.showdialog() == dialogresult.ok)//打开文件对话框 { filestream file_path = new filestream(save_file.filename, filemode.openorcreate,fileaccess.readwrite); binarywriter bin_write = new binarywriter(file_path);//创建bin文件流 byte[] init_byte = new byte[256]; for (int temp = 0; temp < 256; temp++) { init_byte[temp] = (byte)temp; } bin_write.write(init_byte, 0, 256);//初始化bin文件 console.writeline(file_path.length); //看一下目前文件大小 bin_write.seek(255, seekorigin.begin);//修改bin文件当前位置至第255个字节 bin_write.write(0x08); //第255个字节改为08 bin_write.seek(8, seekorigin.begin);//修改bin文件当前位置至第8个字节 bin_write.write((byte)0x01);//第8个字节改为01 bin_write.write((byte)0x02);//第9个字节改为02 bin_write.write((byte)(0x90));//第10个字节改为90 byte[] buffer = new byte[8]; for (int temp = 0; temp < 8; temp++) { buffer[temp] = (byte)(temp + 1); } bin_write.seek(128, seekorigin.begin);//修改bin文件当前位置至第128个字节 bin_write.write(buffer, 2, 5);//将buffer字节数组中的第2到到第7个数赋值到bin文件的第128到133个字节 bin_write.write((byte)(0x90));//第134个字节改为08 console.writeline(file_path.length);//看一下目前的文件大小 file_path.setlength(256);//文件大小已经超过256,只保留256个字节 console.writeline(file_path.length);//看一下目前的文件大小 bin_write.flush();//释放文件资源 bin_write.close(); file_path.close(); } } }}上述代码的运行结果为:
可以看到,bin文件相应的位置已经更改完成,并且其他位置也没有出现变动。
这里我需要提一下,在做替换过程中,bin文件的大小是会发生变化的,因此我用console.writeline(file_path. length)来监控文件的大小变化。 控制台输出的结果为:
256,259,256
因此,我在代码的最后将文件的长度强行设置为256.这个不用担心数据,实际测试下来,如果没有file_path. setlength(256)语句,那么结果如下:
可以看到后面几个数据是无效的数据,这个可以直接去掉。
以上是我平时比较常用的bin文件操作。 当然,bin文件的某一位的删除和插入,我还没有比较容易的办法,不过bin文件的删除或者插入特定字符用的场景非常少,因此没有过多的研究。 希望以上内容对大家有所帮助。

micro LED未来怎么走,还看工程大屏
中国6G卫星发射6G网络开始运营了吗
新一期《光学学报》发表特邀综述,透露我国EUV光刻物镜系统光学设计进展
什么是手机和弦铃声
HT48R05A-1数据存储器
常用的BIN文件操作总结
尽管美国禁令,华为将与三星的差距缩小到3.6%
三峡陕西米脂县首次开发风电项目,新能源米脂姬岔一期50兆瓦风电项目正式开工
谈谈2010中国电子产业走势——冰消雪融,期待暖春
罗姆与SABIC签署协议,收购SABIC PC薄膜板材业务
蓝牙耳机哪家好?口碑最好的蓝牙耳机品牌
怎样利用AVR单片机来测试蓄电池的剩余电量?
外媒大胆判断:明年苹果仍可能在iPhone中使用高通5G芯片
东航云南公司上线RFID无线射频,行李全流程识别
中国人工智能产业链分布,AI独角兽落地竞争白热化
如何将手机投屏到电脑上
有哪些电子元器件封装?
台积电张忠谋称2014年6月让出CEO之位
关于夜狼安防报警服务行业解决方案的分析和介绍
关于温度测量的那些事儿