Saturday, June 27, 2009

How to pad a .BIN file

I recently needed to pad a very small .BIN file of around 1.4KB to fit into a 128KB memory space. I am sure there are much easier ways of doing this than my solution, but it worked for me so I thought I would share. You will need bin2hex and hex2bin.

1. Copy bin2hex and hex2bin to the same directory as your .BIN file.

2. Open a command prompt in Windows. Navigate to your .BIN file directory.

3. Type "bin2hex file.bin file.hex" where file.bin is the input .BIN file and file.hex is an output .HEX file.

4. Type "hex2bin file.hex /Lxxxxxx file2.bin" where file.hex is the .HEX file that was created in step 3, xxxxxx is the length in bytes of the padded output file and file2.bin is the output .BIN file.

For example, if I wanted to pad my file to be 128KB in length, I simply type "hex2bin file.hex /L131072 file2.bin" because 128KB is equal to 128 x 1024 bytes (which is 131072 bytes).


Andy Hefner said...

In Unix or anything resembling it, you'd use the dd command:

# First, create a 128K file of all zeros:
dd if=/dev/zero bs=1024 count=128 of=padded.bin
# Then, write the BIN to the file without truncating the result:
dd if=file.bin of=padded.bin conv=notrunc

These sorts of little chores are why every Windows box I use ends up with Cygwin or MSYS installed on it the first time I have to do any real work.

freezedream said...

Thanks for this, little-scale. I'm sure it will come in handy!

Maxim said...

There's a padbin.exe in here, with GPL source:
and I could probably come up with a totally free version given 5 minutes.

Maxim said...

Aargh, I can't help it...

- intended to have no restrictions
- source included
- Windows EXE included
- you can say "256K" if you want. Or even "0x100K" if you're that way inclined.
- fills with FFs which write to EPROM faster than zeroes. said...

Well, I do not actually imagine it may work.