Home Blog Blog Details

The difference between HEX and BIN files in microcontrollers

May 07 2025
Ampheo

Inquiry

Global electronic component supplier AMPHEO PTY LTD: Rich inventory for one-stop shopping. Inquire easily, and receive fast, customized solutions and quotes.

QUICK RFQ
ADD TO RFQ LIST
In microcontroller development, HEX (Intel HEX) and BIN (binary) files are two common formats used to store and transfer firmware.

In microcontroller development, HEX (Intel HEX) and BIN (binary) files are two common formats used to store and transfer firmware. Here’s a breakdown of their differences, purposes, and when to use each:

The difference between HEX and BIN files in microcontrollers


1. Binary (BIN) File

  • Format: Raw binary data (0s and 1s) representing the exact machine code to be written to the microcontroller's flash memory.

  • Contents:

    • Pure executable code + data (no additional metadata).

    • Addresses are implied by the file offset (e.g., the first byte is loaded at the base address).

  • Pros:

    • Smaller size (no overhead).

    • Directly writable to flash memory (e.g., via STM32 bootloader).

  • Cons:

    • No address/location information (must be specified separately during flashing).

    • No checksums or error detection.

  • Typical Use Cases:

    • Flashing via tools like stm32flashOpenOCD, or USB DFU.

    • OTA (Over-the-Air) updates where bandwidth matters.


2. Intel HEX (HEX) File

  • Format: ASCII text file with addresses, data, and checksums in a standardized format.

  • Contents:

    • Each line contains:

      • Start code (:).

      • Byte countaddressrecord type (data, EOF, etc.).

      • Data payload + checksum.

    • Supports non-contiguous memory regions (gaps are handled explicitly).

  • Pros:

    • Self-contained address information.

    • Built-in error detection (checksums).

    • Human-readable (can be inspected with a text editor).

  • Cons:

    • Larger file size (due to ASCII encoding and metadata).

    • Requires parsing before flashing.

  • Typical Use Cases:

    • Debugging (easier to analyze).

    • Flashing via IDEs (Keil, IAR, STM32CubeProgrammer).

    • EEPROM programming (where addresses matter).


Key Differences

Feature BIN File HEX File
Format Raw binary ASCII text
Address Info Not included (implied) Explicitly included
Size Compact (no overhead) Larger (due to metadata)
Checksums No Yes
Human-Readable No Yes
Flashing Requires base address input Self-contained

When to Use Which?

  • Use BIN:

    • When flashing via bootloaders (e.g., STM32 UART/USB DFU).

    • For OTA updates (minimizes transmission size).

    • When the target address is known and fixed.

  • Use HEX:

    • When debugging or analyzing firmware.

    • When flashing via IDEs (e.g., STM32CubeIDE automatically generates HEX).

    • For EEPROM or non-contiguous memory layouts.


Conversion Between HEX and BIN

  • HEX to BIN:
    Use tools like objcopy (GNU Arm Embedded Toolchain):

    sh
     
    arm-none-eabi-objcopy -I ihex -O binary firmware.hex firmware.bin
  • BIN to HEX:
    Specify the base address (e.g., 0x08000000 for STM32 flash):

    sh
     
    arm-none-eabi-objcopy -I binary -O ihex --adjust-vma 0x08000000 firmware.bin firmware.hex

Example HEX File Snippet

 
:1000000048000020A1010008AD010008B1010008C4
:10001000B5010008B9010008000000000000000020
:0400200000000000B8
:00000001FF
  • :10 = 16 bytes of data.

  • 0000 = Address offset.

  • 00 = Record type (data).

  • C4 = Checksum.


STM32-Specific Notes

  • STM32CubeIDE: By default generates both .hex and .bin files (configurable in project settings).

  • Flashing Tools:

    • ST-Link Utility: Supports both HEX and BIN.

    • STM32CubeProgrammer: Prefers HEX but accepts BIN (requires address input).


Summary

  • BIN: Simpler, smaller, but lacks metadata. Best for production flashing.

  • HEX: Self-documenting, safer for debugging. Used during development.

Choose based on your toolchain and requirements!

Ampheo