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.
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.
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
- 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.
byte will be written using
A message box will appear when it is done converting.
public partial class Form1 : Form
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))
private void button1_Click(object sender, EventArgs e)
string mydocpath = Environment.GetFolderPath(
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)
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 = "";
while ((line = sr.ReadLine()) != null)
binaryval = "";
line = line.Substring(9);
char charArray = line.ToCharArray();
if (charArray.Length > 32)
binaryval = new string(charArray, 0, 32);
byte buffer = StringToByteArray(sbWrite.ToString());
FileStream fs = new FileStream(saveFileDialog1.FileName,
BinaryWriter bw = new BinaryWriter(fs);
catch (IOException ex)