The “Hex File” serves as a crucial component, acting as the bridge between human-readable source code and machine-executable instructions. Despite its significance, the Hex File often remains shrouded in mystery for many beginners and even experienced developers. The reason the hex representation is used is because it is very convenient when working with microcontrollers as one hex character fits into 1 nibble. Hence F is 1111, FF is 1111 1111 and so forth.
A Hex File, short for Intel Hexadecimal Object File, is a standard file format used to represent machine-readable binary data, typically firmware or executable code, for microcontrollers, embedded systems, and other programmable devices since the 1970s. The Hex File contains a hexadecimal representation of the binary data, making it human-readable while still being efficiently processed by microcontrollers.
How The Hexadecimal System Works
The hexadecimal system is very often used in data processing, as it is ultimately a more convenient administration of the binary system. In computer science, data words usually consist of octets, which can be represented as only two-digit hexadecimal numbers instead of eight-digit binary numbers. In contrast to the decimal system, the hexadecimal system with its base as the fourth power of two (16 = 24) is suitable for easier notation of binary numbers, as a fixed number of characters is always required to reproduce the data word. A nibble can be represented exactly with one hexadecimal digit and a byte with two hexadecimal digits.
---
People are used to calculating in the decimal system. The Indo-Arabic numeral system uses ten symbols to dot the numerals (0 to 9). The hexadecimal system, on the other hand, contains sixteen digits. Since the mid-1950s, the letters A to F or A to F have been used as numerals to represent the six additional digits. This goes back to the practice of IBM computer scientists at the time.
To convert a hexadecimal number to a decimal, you have to multiply the individual digits by the respective power of the base. The exponent of the base corresponds to the digit, with a zero assigned to the number before the decimal point. To do this, however, you have to convert the digits A, B, C, D, E, and F into the corresponding decimal numbers 10, 11, 12, 13, 14, 15.
How Microcontrollers Understand Hexadecimal?
Hex is the middle way of human and machine understanding. The software you use to upload code to the microcontrollers converts the Hex into chunks of 0001000000010000000010000100000 and writes them in the correct addresses.
.bin files are straight binary data (and therefore require a special editor to edit or understand by humans) plus when loading them onto a microcontroller we have to specify the address to load the data to.
.hex files are used for putting code into the memory of microcontrollers. This file type contains metadata about where to load the file, the length of the file, the type of stuff being loaded, etc. This makes it possible to manipulate more with a .hex file than is possible with a .bin file.
Yes, we can write blinking an LED as 00010000 000100000000 100001000000 001000000 010000000 010000100000 (we need to mention that in the program with notation such as 0b) and directly write to the microcontrollers. But it will consume a huge time to write and correctly upload code just to blink an LED. That is analogous to punched tapes. We are automating the process with the help of a programmer. In the later part of this article, we will explain that process.
We do write some parts in bits in special situations, such as when working with Pin-Change Interrupt (PCINT).

Hex editor.
Source: https://blog.mh-nexus.de/2014/12/how-to-understand-raw-data-in-a-hex-editor/
Key Components of a Hex File
This is an example of a Hex file:
1 2 3 4 5 | :020000040000FA :1000000083168501FF308600831207309F0085012B :0800100086010608850009289D :02400E00180395 :00000001FF |
A Hex File consists of multiple data records, each representing a specific segment of binary data.
Each data record includes information such as the starting address, data length, record type, and checksum for error detection. Data records (type 00) contain the actual binary data, while other record types provide metadata and control information. Binary data in a Hex File is represented using hexadecimal notation (base-16), where each byte of data is represented by two hexadecimal digits (0-9 and A).
Hex Files are typically generated through a multi-step process involving compilation, linking, and conversion.
The source code written in a high-level programming language, such as C or assembly, is compiled into object code specific to the target microcontroller architecture. The compiler translates the source code into machine-readable instructions, generating object files (.obj or .o) containing machine code and symbol information.
The linker combines multiple object files and resolves symbol references to create an executable program.
During linking, the linker assigns memory addresses to program sections, resolves external dependencies, and generates a linked output file. This is an example of a linker script:
1 2 3 4 5 6 7 8 9 10 11 12 | /* memory_map.ld */ MEMORY { bootrom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00004000 approm (rx) : ORIGIN = 0x00004000, LENGTH = 0x0003C000 ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000 } __bootrom_start__ = ORIGIN(bootrom); __bootrom_size__ = LENGTH(bootrom); __approm_start__ = ORIGIN(approm); __approm_size__ = LENGTH(approm); |
The linked output file, typically in binary format, is converted into a Hex File using a tool called an Object File Converter. The converter reads the binary data from the linked output file, formats it into hexadecimal records according to the Intel Hex File specification, and writes the records to the Hex File.
Practical Significance of Hex Files
Hex Files play a vital role in the development and deployment of firmware for microcontrollers and embedded systems.
Hex Files serve as the input for microcontroller programming tools, such as Flash programmers or In-Circuit Programmers (ICPs), used to program firmware onto microcontroller devices. The programming tool reads the Hex File and transfers the binary data into the memory of the target microcontroller, enabling it to execute the programmed instructions.
Hex Files facilitates firmware updates and distribution by providing a standardized format for storing and transferring firmware images. Manufacturers and developers can distribute Hex Files to end-users for updating firmware in embedded devices, ensuring compatibility and consistency across different platforms.
Hex Files can be used for debugging and analysis purposes, allowing developers to inspect the generated machine code, identify memory usage patterns, and analyze program behaviour. Tools such as disassemblers or debuggers can parse Hex Files to extract assembly code and symbols for further analysis.