Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / C#
Tip/Trick

Intel Hex to Binary (DATA-only) Program C# GUI

Rate me:
Please Sign up or sign in to vote.
4.47/5 (8 votes)
30 Dec 2013CPOL2 min read 35.3K   43   10   4
This program converts Intel Hex files to binary files.

Introduction 

This program takes an Intel Hex file, extracts ONLY the data (no address, crc, etc. included), and converts the data to a binary file. This program WILL NOT convert the entire Intel Hex file to binary. The program does not handle exceptions and may include bugs if you do not follow the sequence. I simply made it for my own purposes and thought I would share. (: I would also like to note this program is similar to one I found on Google. I haven't been able to relocate the website, but when I do I will post it.

 If you're not familiar with an Intel Hex file, go to http://en.wikipedia.org/wiki/Intel_HEX

Background   

I needed to test if I was uploading a .bin application to my microcontroller properly. The flash dump was formatted into an Intel Hex file. So I needed to extract the data from the Intel Hex file and convert it to .bin file. This does allow you to select multiple files. I couldn't find any programs that were free or worked for me, so I made my own. It's not the best, but it does the job.   

Program Code 

Click the Convert Hex to Bin button. Select Intel Hex files that you want to convert. To convert the hex file to a bin file, each Intel Hex file goes through this process: 

  1. Edit out the first and last two lines of the file. 
  2. Edit out the :10/appended address/crc 
  3. Use stringbuilder to build hex data string. 
  4. Convert edited data into a byte[] 
  5. A message box will appear and will tell you which .hex file was read.
  6. A save file dialog will appear. You will specify where you want to put the file, and you MUST name the file with the .bin extension!!! Otherwise, I don't know if this will work. So you would type "choosebinfilename.bin". This will create the .bin file. 
  7. The byte[] will be written using BinaryWriter

A message box will appear when it is done converting.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private const byte semicolon = 0x3A;
        private byte[] mBuffer = null;
        private string test = "";

        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 = 2;
            openFileDialog1.Multiselect = true;
            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;

                            //Get rid of first line
                            sr.ReadLine();

                            while ((line = sr.ReadLine()) != null)
                            {
                                binaryval = "";
                                line = line.Substring(9);
                                char[] charArray = line.ToCharArray();

                                if (charArray.Length > 32)
                                {
                                    binaryval = new string(charArray, 0, 32);
                                    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)
                {
                }
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to create Intel Hex file from byte array? Pin
S K Suman17-Jun-19 8:30
S K Suman17-Jun-19 8:30 
QuestionSkipping lines Pin
GSok7-Feb-15 0:05
GSok7-Feb-15 0:05 
QuestionHello, can you share this Project? Pin
Member 1141043228-Jan-15 18:05
Member 1141043228-Jan-15 18:05 
QuestionVery good article! Pin
Volynsky Alex30-Dec-13 9:37
professionalVolynsky Alex30-Dec-13 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.