读一个十六进制的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 文件的更多相关文章

  1. 如何将一个二进制的xxx.bin文件轻松转为C语言数组

    今天在写一个SPI-flash读写程序,目的是要将一个二进制文件写到SPI_FLASH中,最后通过开机读取,实际上这个.bin文件就是uboot和second-boot的结合体.通过SD卡写到SPI- ...

  2. 使用Spark读写CSV格式文件(转)

    原文链接:使用Spark读写CSV格式文件 CSV格式的文件也称为逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号.在本文中的CSV格 ...

  3. Keil如何生成bin文件【Keil生成Bin文件的方法】

    使用过Keil的同鞋都知道,现在Keil中默认可以输出.axf的调试文件和可以通过钩选输出的.hex可执行文件,没有bin(二进制)文件的输出选项.可是偏偏某些时候需要或者习惯性的使用.bin文件来进 ...

  4. S03_CH12_基于UDP的QSPI Flash bin文件网络烧写

    S03_CH12_基于UDP的QSPI Flash bin文件网络烧写 12.1概述 为了满足不同的需求,本例程在"基于TCP的QSPI Flash bin文件网络烧写"上进行修改 ...

  5. S03_CH11_基于TCP的QSPI Flash bin文件网络烧写

    S03_CH11_基于TCP的QSPI Flash bin文件网络烧写 11.1概述 针对ZYNQ中使用QSPI BOOT的应用,将BOOT.bin文件烧写至QSPI Flash基本都是通过USB C ...

  6. java 读写properties (配置)文件

    Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...

  7. STM32 Unicode 与 GBK 转换 .bin文件放到SD卡是啥意思

    2个数组 : }; }; 一个是Unicode 编码,一个是GBK编码: 用c2b软件转成.bin 二进制文件放到SD卡里: SD卡放入字库 .FON STM32 代码: 代码中SD卡字库和二进制路径 ...

  8. 处理bin文件

    1. fs.Position  写入的位置,从哪个位置开始写 fs.Write(byte1,0,byte1.Length); byte1写入的byte[], 写入内容从第几位开始取,length取多长 ...

  9. JLINK通过JFLASH烧写bin文件报错处理方法

    错误原因:烧写开始地址出错,打开BIN文件后弹出的设置开始地址不正确不能为0 解决措施:用J-FLASH LITE或者将开始地址设置成正确的地址(KEILMDK中IROM1的开始地址

随机推荐

  1. JSF页面中使用js函数回调后台bean方法并获取返回值的方法

    由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...

  2. vim关于 引号和 括号的 高效操作-很好很强大的!

    http://blog.csdn.net/bigshady/article/details/6019963 对括号匹配, 进行跳转, 使用的是%. 匹配的括号, 都会被高亮显示, 但是: 根据光标的 ...

  3. 关于动态URL地址设置静态形式

    动态URL地址:http://station.com/index.php?c=play&a=index&id=12345 静态URL地址:http://station.com/play ...

  4. AngularJS API之equal比较对象

    使用情况 1 首先,所有满足 a === 3 这种的对象,在angular.equals(a,b)中都会返回真 2 所有对象的类型,以及属性值都相同的,也会返回真 3 NaN和NaN也会返回真(在ja ...

  5. 一个简单的猜大小的小游戏 python

    初学python,用python写了一个简单的猜大小的小游戏 #!/usr/bin/env python #-*- coding:utf-8 -*- print "------------- ...

  6. Entity Framework 之Database first(数据库优先)&Model First(模型优先)

    一.什么是Entity Framework 1.1 实体框架(EF)是一个对象关系映射器,使.NET开发人员使用特定于域的对象与关系数据.它消除了需要开发人员通常需要编写的大部分数据访问代码.简化了原 ...

  7. UNITY3D在线更新之道-CSlight 使用总结

    转自:http://blog.csdn.net/leonwei/article/details/39233775 最近做U3D的热更新,研究了各种方式无果后,最容易最先想到的方式就是利用c#的反射机制 ...

  8. matlab随笔(三)

    把矩阵变成行向量(矩阵元素的排列是从上到下,从左到右): 1.先转置,转成行向量 A = >> A=A' A = >> a=A(:)' a = 2.reshape函数 A = ...

  9. Mac之vim普通命令使用[转]

    高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx     开始记录宏,并将结果存入寄存器xq     退出记录模式@x     播放记录在x寄存器中的宏命 ...

  10. COGS 2434 暗之链锁 题解

    [题意] 给出一个有n个点的无向图,其中有n-1条主要边且这些主要边构成一棵树,此外还有m条其他边,求斩断原图的一条主要边和一条其他边使得图不连通的方案数. 注意,即使只斩断主要边就可以使得原图不连通 ...