개발/C#

C# Intel hex to bin Code

FA1976 2018. 1. 2. 16:34

Intel Hex file 을 bin 파일로 변경해주는 코드.

winform 형태로 테스트를 위해서는 button1과 saveFileDialog1만 별도 이름 변경없이 추가만 하면됨.


1. 파일을 불러온다.

2. 정상적으로 불러왔으면 파일 이름이 messagebox에 뜬다.

3. 확인을 누르면 bin 파일로 변경된 파일을 저장할곳을 지정한다.

4. 완료.


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 Hex2bin

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        public static byte[] StringToByteArray(string hex)

        {

            return Enumerable.Range(0, hex.Length)

                             .Where(x => x % 2 == 0)

                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))

                             .ToArray();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            Stream myStream;

            string mydocpath = Environment.GetFolderPath(

                     Environment.SpecialFolder.MyDocuments);

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = mydocpath;

            openFileDialog1.Filter = "hex files (*.hex)|*.hex|All files (*.*)|*.*";

            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = false;

            openFileDialog1.RestoreDirectory = true;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                try

                {

                    string tempFolder = System.IO.Path.GetTempPath();

                    foreach (string fileName in openFileDialog1.FileNames)

                    {

                        using (StreamReader sr = new StreamReader(fileName))

                        {

                            StringBuilder sbWrite = new StringBuilder();

                            String binaryval = "", hexvalue = "";


                            String line;


                            while ((line = sr.ReadLine()) != null)

                            {

                                if(line.Substring(7,2)== "01")

                                {// end of file

                                    break;

                                }

                                else if (line.Substring(7, 2) == "00")

                                {// data

                                    binaryval = "";

                                    line = line.Substring(9);

                                    char[] charArray = line.ToCharArray();


                                    if (charArray.Length > 32)

                                    {

                                        binaryval = new string(charArray, 0, 32);

                                    }

                                    else

                                    {

                                        binaryval = new string(charArray, 0, charArray.Length-2);

                                    }


                                    sbWrite.Append(binaryval);

                                }


                            }


                            byte[] buffer = StringToByteArray(sbWrite.ToString());


                            MessageBox.Show(fileName);

                            saveFileDialog1.ShowDialog();

                            FileStream fs = new FileStream(saveFileDialog1.FileName,

                                FileMode.Create, FileAccess.ReadWrite);

                            BinaryWriter bw = new BinaryWriter(fs);

                            bw.Write(buffer);

                            bw.Close();

                            fs.Close();

                        }

                    }


                    MessageBox.Show("Conversion complete.");

                }

                catch (IOException ex)

                {

                }

            }

        }

    }

}