使用VS2008 编写一个修改hex文件的工具

1652阅读 0评论2010-05-10 waderwang
分类:WINDOWS


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace PowerBoardIDmodifier
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.hex)|*.hex";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((openFileDialog1.OpenFile()) != null)
                    {
                        textBox1.Text = openFileDialog1.FileName;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }


        //Modify the cresponding file.
        private void button2_Click(object sender, EventArgs e)
        {
            
            if(textBox1.Text == "")
            {
                MessageBox.Show("The file name should not empty!");
                return;
            }
            
            
            
            StreamReader sr = File.OpenText(textBox1.Text);

            //jump the first 34 lines.
            for (int i = 0; i < 34;i++)
                sr.ReadLine();

            //Get the 35th line.
            String str = sr.ReadLine();
            sr.Close();




            //Board ID
            int iBoardID = int.Parse(textBox3.Text,System.Globalization.NumberStyles.Integer);
            iBoardID += 0x43000000;
            iBoardID = IPAddress.HostToNetworkOrder(iBoardID);
            String BoardIDstr = iBoardID.ToString("X8");

            //OEM ID
            short iOEMID = short.Parse(textBox2.Text, System.Globalization.NumberStyles.HexNumber);
            iOEMID = IPAddress.HostToNetworkOrder(iOEMID);
            String OEMIDstr = iOEMID.ToString("X4");



            char [] CmdStr = new char[100];

            str.CopyTo(0,CmdStr,0,str.Length);

            //Copy the BoardID to the array
            BoardIDstr.CopyTo(0, CmdStr, 17, BoardIDstr.Length);

            //Copy the OEMID to the array
            OEMIDstr.CopyTo(0, CmdStr, 9, OEMIDstr.Length);


            //Calculate the checksum. :10020000F35304000F04004301000000000000004D
            int iCheckSum = 0x00;
            for (int i = 1; i < str.Length - 2; i+=2)
            {
                String subStr = ConvertCharArrayToString(CmdStr,i,2);
                iCheckSum += int.Parse(subStr, System.Globalization.NumberStyles.HexNumber);
            }

            iCheckSum = (~iCheckSum + 0x01)&0xFF;
            Byte byCheckSum = (Byte)iCheckSum;
            String CheckSumStr = byCheckSum.ToString("X2");
            CheckSumStr.CopyTo(0,CmdStr, str.Length - 2, 2);


            //Get the result string.
            String NewStr = ConvertCharArrayToString(CmdStr, 0, CmdStr.Length);
            

            //Write the str back to the file.
            FileInfo f = new FileInfo("power_board_update_" + textBox2.Text +"_" + textBox3.Text + ".hex");
            StreamWriter wr = f.CreateText();
            sr = File.OpenText(textBox1.Text);

            int Cnt = 0;
            string TempStr = null;
            while ((TempStr = sr.ReadLine()) != null)
            {
                if (Cnt++ == 34)
                    wr.WriteLine(NewStr);
                else
                    wr.WriteLine(TempStr);
            }

            sr.Close();
            wr.Close();

            MessageBox.Show("Covert success!");

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private String ConvertCharArrayToString(char[] array, int startIndex, int len)
        {
            String NewString = "";

            for (int i = startIndex; i < startIndex + len; i++)
            {
                if (array[i] == '\0')
                    break;
                
                NewString += array[i];
            }

            return NewString;
        }

    }
}


上一篇:u-boot 烧写yaffs文件系统时ood要互换位置?
下一篇:C# winform实现从控制台输出