Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

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

, 30 Dec 2013
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Nicole Jordan

United States United States
No Biography provided

Comments and Discussions

 
QuestionVery good article! PinmemberVolynsky Alex30-Dec-13 9:37 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 30 Dec 2013
Article Copyright 2013 by Nicole Jordan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid