如何打开bin文件(如何打开bin文件不乱码)

  • 生活
  • 2023-04-19 15:42

BIN文件,即二进制文件,广泛应用于嵌入式,我们常用的Firmware通常会以BIN文件或者HEX文件格式存储,因此,对BIN文件的读写操作其实还是很普遍的,在这里,我记录一下我常用到的BIN文件操作。

首先C#Winform中有Binary文件(BIN文件)的基本操作类。如下所示

FileStreamfile_path=newFileStream(文件名,FileMode,FileAccess);//BinaryReaderbin_read=newBinaryReader(file_path);BinaryWriterbin_write=newBinaryWriter(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,intindex,intcount);这个***的含义就是将buffer数组中的一部分值(buffer数组的开始索引为index,长度为count),赋值至BIN文件当前位置。

下面我举一个例子,BIN文件的写,从0写到255,256个byte。

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO;namespaceTEST{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){SaveFileDialogsave_file=newSaveFileDialog();save_file.Filter="BIN文件|*.bin";if(save_file.ShowDialog()==DialogResult.OK){FileStreamfile_path=newFileStream(save_file.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);BinaryWriterbin_write=newBinaryWriter(file_path);//创建BIN文件流byte[]init_byte=newbyte[256];for(inttemp=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文件,那么替换功能将会非常有用,比如将其中的某几个数字改动一下,见代码:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO;namespaceTEST{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){SaveFileDialogsave_file=newSaveFileDialog();save_file.Filter="BIN文件|*.bin";if(save_file.ShowDialog()==DialogResult.OK)//打开文件对话框{FileStreamfile_path=newFileStream(save_file.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);BinaryWriterbin_write=newBinaryWriter(file_path);//创建BIN文件流byte[]init_byte=newbyte[256];for(inttemp=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个字节改为08bin_write.Seek(8,SeekOrigin.Begin);//修改BIN文件当前位置至第8个字节bin_write.Write((byte)0x01);//第8个字节改为01bin_write.Write((byte)0x02);//第9个字节改为02bin_write.Write((byte)(0x90));//第10个字节改为90byte[]buffer=newbyte[8];for(inttemp=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个字节改为08Console.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文件的删除或者插入特定字符用的场景非常少,因此没有过多的研究。希望以上内容对大家有所帮助。

猜你喜欢