常用的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文件的第128133个字节
                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文件的删除或者插入特定字符用的场景非常少,因此没有过多的研究。 希望以上内容对大家有所帮助。

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分