码迷,mamicode.com
首页 > Windows程序 > 详细

C# 读写十六进制bin 文件

时间:2016-07-08 17:55:42      阅读:1513      评论:0      收藏:0      [点我收藏+]

标签:

  读一个十六进制的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 Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string vendorCode = "";
        private string deviceType = "";
        private string hardwareVersion = "";
        private string crc32 = "";
        private string firmwareLength = "";

        private string sourceBinFile = "";
        private string saveBinFile = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            buttonGenerate.Enabled = false;
            lblMsg.Text = "";
            lblMsg.ForeColor = Color.Empty;
        }
        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)
        {
            lblMsg.Text = "";
            lblMsg.ForeColor = Color.Empty;

            vendorCode = RightAddZero(textBoxVendorCode.Text.Trim(), 2);
             deviceType = LeftAddZero(textBoxDeviceType.Text.Trim(), 18);
             hardwareVersion = RightAddZero(textBoxHardwareVersion.Text.Trim(), 4);
             crc32 = RightAddZero("CRC3", 4);


            OpenFileDialog dialog = new OpenFileDialog();           
            dialog.Filter = "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*";
            dialog.Title = string.Format("Open Firmware File");
            dialog.FilterIndex = 0;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() != DialogResult.Yes)
            {
                sourceBinFile = dialog.FileName;
                textBoxBinaryPath.Text = sourceBinFile;
                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()
        {
            byte[] binContent = File.ReadAllBytes(sourceBinFile);
            int binLength = binContent.Length;
            firmwareLength = RightAddZero(binContent.Length.ToString(),4);

            //byte[] buffer = System.Text.Encoding.ASCII.GetBytes("NTABCD");
            using (FileStream fs = new FileStream(saveBinFile, FileMode.Create, FileAccess.Write))
            {
                string headerRow0 = vendorCode + deviceType.Substring(0, 14);
                WirteBin(fs, headerRow0, 0);
                string headerRow1 = deviceType.Substring(13) + hardwareVersion;
                WirteBin(fs, headerRow1, 1);
                string headerRow2 = crc32 + firmwareLength;
                WirteBin(fs, headerRow2, 2);
                string headerRow3 = "\0";
                WirteBin(fs, headerRow3, 3);

                   byte[] binBytes=new byte[16];
                   int j = 0;
                   int rows = 0;
                   for(int i=0;i< binLength; i++)
                   {
                        binBytes[j] = binContent[i];
                        j++;
                         //16字节一行
                         if (j % 16 == 0)
                         {
                            rows++;
                            fs.Position = 16 * (rows+3);
                            fs.Write(binBytes, 0, 16);
                            j = 0;
                         }

                   }
                 
                

            }

        }

        private void WirteBin(FileStream fs,string str,int row)
        {
            
            byte[] buffer  = System.Text.Encoding.ASCII.GetBytes(str);
            fs.Position = 16 * row;
            fs.Write(buffer, 0, str.Length);
        }


        private void buttonGenerate_Click(object sender, EventArgs e)
        {
        

            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*";
            saveDlg.Title = string.Format("Save Binary File");
            saveDlg.FilterIndex = 0;
            saveDlg.RestoreDirectory = true;
            if (saveDlg.ShowDialog() != DialogResult.Yes)
            {
                saveBinFile = saveDlg.FileName;
                textBoxBinaryPath.Text = saveBinFile;
                try
                {                   
                    ReadWirteBinFile();
                    lblMsg.Text = "It‘s ok!";
                    lblMsg.ForeColor = Color.LightSeaGreen;
                    buttonGenerate.Enabled = false;

                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }

        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;
        }

      
    }
}

 

C# 读写十六进制bin 文件

标签:

原文地址:http://www.cnblogs.com/ike_li/p/5653969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!