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:
- Edit out the first and last two lines of the file.
- Edit out the :10/appended address/crc
- Use stringbuilder to build hex data string.
- Convert edited data into a
byte[]
- A message box will appear and will tell you which .hex file was read.
- 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.
- 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;
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)
{
}
}
}
}
}