🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Hexadecimal Number System in Embedded C: Complete Guide
Learn why datasheets, debuggers and header files speak hexadecimal. Master the 4-bit grouping method, hex literals in C, STM32F407 addresses, masks, memory dumps and little-endian byte order.
What You Will Learn
Key Terms
1. Why the Hexadecimal Number System Is Used in Embedded C
The hexadecimal number system in embedded C is the everyday notation of firmware engineers. Every register address in a reference manual, every value in a debugger’s memory window and every constant in a header file such as stm32f407xx.h is written in hex. Binary is what the hardware stores; hexadecimal is how humans read and write binary without losing their place.
Look at one 32-bit value, the address of the RCC AHB1 clock-enable register on the STM32F407. Both lines below are the same number:
The 32 ones and zeros are almost impossible to read, copy or compare reliably. The hex form 0x40023830 has only eight characters. The reason is simple: one hex digit represents exactly four bits, so a 32-bit value always needs exactly 8 hex digits, and a byte always exactly 2. No other base lines up with the bit grouping so neatly:
| Representation | Digits for a 32-bit value | Maps to bits cleanly? | Typical use |
|---|---|---|---|
| Binary (base 2) | 32 | Yes, 1 digit = 1 bit | Bit-level reasoning, short fields |
| Octal (base 8) | 11 | No, 3 bits per digit do not divide 8/16/32 | Unix file permissions |
| Decimal (base 10) | 10 | No, no relation to bit boundaries | Human quantities: 100 ms, 9600 baud |
| Hexadecimal (base 16) | 8 | Yes, 1 digit = 4 bits (a nibble) | Addresses, registers, masks, memory dumps |
2. Hexadecimal Digits and Base-16 Place Values
Base 16 needs sixteen different digit symbols. We have only ten (0–9), so the letters A to F stand for the values 10 to 15. Upper and lower case mean the same (0xAB equals 0xab), but datasheets and most firmware style guides use upper case.
| Decimal | Hex digit | 4-bit binary | Note |
|---|---|---|---|
| 0 | 0 | 0000 | |
| 1 | 1 | 0001 | |
| 2 | 2 | 0010 | |
| 3 | 3 | 0011 | |
| 4 | 4 | 0100 | |
| 5 | 5 | 0101 | |
| 6 | 6 | 0110 | |
| 7 | 7 | 0111 | |
| 8 | 8 | 1000 | |
| 9 | 9 | 1001 | |
| 10 | A | 1010 | ten |
| 11 | B | 1011 | eleven |
| 12 | C | 1100 | twelve |
| 13 | D | 1101 | thirteen |
| 14 | E | 1110 | fourteen |
| 15 | F | 1111 | fifteen |
Positional notation works exactly like in decimal, except each column is worth 16 times the column on its right. The columns have the weights 1, 16, 256, 4096, 65536 … (powers of 16). Here is 0x3E8 dissected:
3. Hex to Binary Conversion: The 4-Bit Grouping Method
This is the single most useful skill of this lesson. Because 16 = 24, every hex digit corresponds to exactly one nibble (4 bits). To convert hex to binary you replace each digit independently by its 4-bit pattern, no arithmetic required.
Example: 0xB5 → binary
Result: 0xB5 = 1011 0101 (= 181 decimal). The digit B is 11, so its nibble is 8 + 2 + 1 = 1011.
Example: 0x3F8A → binary
Binary to hex: group from the RIGHT
Starting from the least significant bit, cut the binary string into groups of four, pad the leftmost group with zeros if necessary, and replace each group by its hex digit. Consider the 7-bit number 1011010:
1011 010 and must pad on the right, which silently changes the value. Always group from the right (the LSB end) and pad on the left.Mental conversion table: learn these 16 patterns
Memorise 8-4-2-1 and the rest follows: 6 = 4+2 = 0110, 9 = 8+1 = 1001, C = 8+4 = 1100, E = 8+4+2 = 1110, F = 8+4+2+1 = 1111.
4. Hex to Decimal and Decimal to Hex Conversion
Hex to decimal: multiply each digit by its weight
| Hex | Calculation | Decimal | Where it appears |
|---|---|---|---|
| 0xFF | 15×16 + 15 | 255 | Largest byte |
| 0x3E8 | 3×256 + 14×16 + 8 | 1000 | Timer count for 1 ms at 1 MHz (+1) |
| 0x1A3F | 1×4096 + 10×256 + 3×16 + 15 | 6719 | Arbitrary 16-bit value |
| 0x8000 | 8×4096 | 32768 | Bit 15 only |
| 0xFFFF | 15×4096 + 15×256 + 15×16 + 15 | 65535 | Largest 16-bit value |
| 0x0800 0000 | 8×166 | 134217728 | Start of STM32F407 Flash |
Decimal to hex: repeated division by 16
The method is the same as for binary, but you divide by 16. The remainder (0 to 15) is converted to a hex digit; remainders 10–15 become A–F. Convert 6719:
| Step | Divide | Quotient | Remainder |
|---|---|---|---|
| 1 | 6719 ÷ 16 | 419 | F |
| 2 | 419 ÷ 16 | 26 | 3 |
| 3 | 26 ÷ 16 | 1 | A |
| 4 | 1 ÷ 16 | 0 | 1 |
Reading the remainders from bottom to top gives 1 A 3 F, so 6719 = 0x1A3F, which matches the previous example in reverse.
printf("0x%X", 6719) and read the output.5. The 0x Prefix, Hex Literals and printf Formats in C
In C a hexadecimal constant starts with 0x or 0X, followed by the digits 0–9, A–F. Without the prefix, the compiler treats the digits as decimal; with a leading 0 only, as octal (the trap from Lesson 2.1).
| Notation | Example | Value | Comment |
|---|---|---|---|
| Decimal | 255 | 255 | No prefix |
| Hexadecimal | 0xFF, 0XFF, 0xff | 255 | Prefix 0x; digits are case-insensitive |
| Octal | 0377 | 255 | Leading zero only. Avoid! |
| Binary (extension / C23) | 0b11111111 | 255 | GCC extension; official in C23 |
| Unsigned suffix | 0xFFu, 0xFFFFFFFFUL | 255 / 4294967295 | U = unsigned, L = long |
Which type does a hex literal get?
Unlike a decimal constant, a hexadecimal constant may be given an unsigned type automatically: the compiler picks the first type from int, unsigned int, long, unsigned long, … that can hold the value. This is why 0x80000000 behaves differently from 2147483648 and from -2147483648.
long): 0x100000000 does not fit in a 32-bit long, so the compiler would choose long long there. The first four lines are identical on both platforms. This is one more reason to write masks with explicit suffixes: 0x80000000U.Printing hex with printf
| Format | Meaning |
|---|---|
%x / %X | Hex, lower / upper case, no prefix, no padding |
%#x | Adds the 0x prefix (but not for zero) |
%08X | Eight digits, zero padded: the standard way to print a 32-bit register |
%02X | Two digits: a single byte in a hex dump |
%p | Pointer value; prints an address (format is implementation defined) |
%lX, %llX | For long and long long arguments |
Parsing hex text
6. Hexadecimal Addresses in the STM32F407 Memory Map
The Cortex-M4 has a single 32-bit address space (4 GiB, 0x00000000 to 0xFFFFFFFF). Flash, RAM and every peripheral register live in that one space, a design called memory-mapped I/O. Hex makes the structure visible: the top digits select the region.
Anatomy of one register address
To drive the green LED we write to GPIOD_ODR. Its address is the base address of the GPIOD peripheral plus the offset of the ODR register:
Each GPIO port occupies a 1 KB block (0x400). Port A starts at 0x40020000, B at +0x400, C at +0x800, D at +0xC00. All registers of one port are 32 bits wide and sit at 4-byte offsets:
| Register | Offset | Address (GPIOD) | Purpose |
|---|---|---|---|
| GPIOD_MODER | 0x00 | 0x40020C00 | Mode (input / output / AF / analog) |
| GPIOD_OTYPER | 0x04 | 0x40020C04 | Output type (push-pull / open-drain) |
| GPIOD_OSPEEDR | 0x08 | 0x40020C08 | Output speed |
| GPIOD_PUPDR | 0x0C | 0x40020C0C | Pull-up / pull-down |
| GPIOD_IDR | 0x10 | 0x40020C10 | Input data (read pins) |
| GPIOD_ODR | 0x14 | 0x40020C14 | Output data (drive pins) |
| GPIOD_BSRR | 0x18 | 0x40020C18 | Bit set / reset (atomic) |
| GPIOD_LCKR | 0x1C | 0x40020C1C | Configuration lock |
| GPIOD_AFRL | 0x20 | 0x40020C20 | Alternate function, pins 0-7 |
| GPIOD_AFRH | 0x24 | 0x40020C24 | Alternate function, pins 8-15 |
Common peripheral base addresses
| Peripheral | Base address | Bus |
|---|---|---|
| TIM2 | 0x40000000 | APB1 |
| USART2 | 0x40004400 | APB1 |
| I2C1 | 0x40005400 | APB1 |
| USART1 | 0x40011000 | APB2 |
| ADC1 | 0x40012000 | APB2 |
| SPI1 | 0x40013000 | APB2 |
| GPIOA | 0x40020000 | AHB1 |
| GPIOB | 0x40020400 | AHB1 |
| GPIOC | 0x40020800 | AHB1 |
| GPIOD | 0x40020C00 | AHB1 |
| GPIOE | 0x40021000 | AHB1 |
| RCC | 0x40023800 | AHB1 |
GPIOD_BASE. You will rarely type them yourself, but you must recognise them in the debugger and in linker maps.7. Hex Masks: Extracting Nibbles and Bytes
Hex is the natural language of bit masks. A mask is a constant whose 1-bits select the bits you care about; F selects a whole nibble, FF a whole byte, so you can read the mask directly.
| Mask | Binary pattern | Selects |
|---|---|---|
0x0F | 0000 1111 | Low nibble of a byte |
0xF0 | 1111 0000 | High nibble of a byte |
0xFF | 1111 1111 | One byte |
0xFF00 | 1111 1111 0000 0000 | Byte 1 of a 16/32-bit value |
0x0000FFFF | 16 ones | Lower half-word |
0x80000000 | 1 followed by 31 zeros | Bit 31 (MSB of a 32-bit word) |
0x00000001 | 31 zeros then 1 | Bit 0 (LSB) |
How AND with a mask works
Reading the result is trivial in hex: 0xB5C3A1F0 has the nibbles B, 5, C, 3, A, 1, F, 0 from top to bottom. The top nibble is B, byte 1 is A1, and so on. Doing the same in decimal would be hopeless.
8. Hex Dumps, the Memory Browser and Little-Endian Byte Order
When you open the Memory view in STM32CubeIDE you see rows of bytes in hex, each row prefixed with its address. Because a byte is exactly two hex digits, memory dumps are compact and aligned. A 32-bit variable occupies four consecutive byte addresses.
The ARM Cortex-M4 is little-endian: the least significant byte of a multi-byte value is stored at the lowest address. A uint32_t holding 0x12345678 at address 0x20000000 looks like this in memory:
78 56 34 12 and must mentally reverse it to get the value 0x12345678. Many debuggers also offer a “32-bit integer” display mode that does this for you.9. Hexadecimal Arithmetic for Addresses and Offsets in Embedded C
You rarely do long hex arithmetic, but you must be able to add an offset to a base address and to find the end of a memory region. The rule is the same as decimal: add column by column, carry when the digit sum reaches 16.
Useful region calculations
| Question | Calculation | Answer |
|---|---|---|
| Last Flash address (1 MB) | 0x08000000 + 0x100000 − 1 | 0x080FFFFF |
| End of SRAM1 + SRAM2 (128 KB = 0x20000) | 0x20000000 + 0x20000 | 0x20020000 (initial stack pointer) |
| Address of GPIOC | 0x40020000 + 2 × 0x400 | 0x40020800 |
| Size of region 0x20000000 – 0x2001BFFF | 0x1C000 = 114,688 | 112 KB (SRAM1) |
| Address of the 5th 32-bit element of an array at 0x20000100 | 0x20000100 + 5 × 4 | 0x20000114 |
printf("0x%08X", 0x40020C00u + 0x14u); prints 0x40020C14.10. Try It: Interactive 16-bit Hex Explorer
Click bits to see how groups of four bits become hex digits. Try to make 0xF000 (the four LED bits of GPIOD_ODR), then 0x0537, then 0x8000.
11. Real-World Uses of the Hexadecimal Number System in Embedded C
| Use case | Example | Why hex? |
|---|---|---|
| Register addresses | 0x40023830 (RCC_AHB1ENR) | Datasheet notation; shows bus and block structure |
| Bit masks | GPIOD->ODR &= ~0xF000; | Four F digits = four bits; readable mask |
| Device I2C addresses | MPU6050 at 0x68, BMP280 at 0x76 | Vendors publish 7-bit addresses in hex |
| Protocol bytes | CAN ID 0x7DF, command 0xA5 | Frames are specified as hex byte sequences |
| Colours (LCD) | RGB565 white = 0xFFFF | Fixed-width fields |
| Magic numbers | 0xDEADBEEF, 0xA5A5A5A5 | Easy to spot in a memory dump (stack fill, error marker) |
| Linker script and map files | ORIGIN = 0x08000000, LENGTH = 1024K | Addresses are always hex |
| Checksums and CRC | CRC-32 result 0xCBF43926 | Compact representation of 32 bits |
12. Common Mistakes With Hexadecimal in C
int mask = FF; is a compile error, but int addr = 4002; compiles and gives decimal 4002, not hex 0x4002. Always prefix.printf("%d", 0xFFFFFFFF) prints −1 for a 32-bit int. Use %u or %X and an unsigned type.0xFOO is a syntax error; the zero is 0.~0xFF on a 16-bit value does not give 0xFF00 but 0xFFFFFF00 after integer promotion. Mask the result: (~0xFFu) & 0xFFFFu.78 56 34 12 mean 0x12345678, not 0x78563412.13. Best Practices for the Hexadecimal Number System in Embedded C
Applying the hexadecimal number system in embedded C consistently makes firmware easier to review, debug and port. These habits are the ones professional teams enforce in code review:
- Write addresses, masks and register values in hex; write quantities in decimal.
- Use upper-case hex digits and a lower-case
x:0x3F8A. - Add U / UL suffixes to masks that can reach bit 31:
0x80000000U. - Always zero-pad when printing registers:
%08Xfor 32-bit,%04Xfor 16-bit,%02Xfor 8-bit. - Prefer named constants over raw hex in code:
#define GPIOD_BASE 0x40020C00UL. Raw hex is for the definition, names are for the use. - Document bit meaning next to the hex constant:
0xC0 /* TXE | TC */. - Verify odd conversions with the compiler or the debugger’s expression window instead of trusting mental arithmetic late in the day.
Key Takeaways — Lesson 2.2
- The hexadecimal number system in embedded C is base 16 with digits 0–9 and A–F.
- One hex digit = four bits = one nibble; one byte = two hex digits; one 32-bit word = eight hex digits.
- Hex to binary: replace every digit with its 4-bit pattern. Binary to hex: group from the right.
- Hex to decimal: digit × 16position. Decimal to hex: repeated division by 16.
- In C,
0xmarks hex, a lone leading0means octal, and hex literals can have an unsigned type. - STM32F407 addresses are base + offset; GPIOD_ODR = 0x40020C00 + 0x14 = 0x40020C14.
- Cortex-M4 is little-endian: the least significant byte is at the lowest address.
- Mastering the hexadecimal number system in embedded C is a prerequisite for reading any reference manual, linker map or debugger window.
Frequently Asked Questions
Ready for hands-on hex practice?
The next page works through hex conversions, address breakdowns, hex arithmetic and byte-nibble-word diagrams step by step.

2 Comments