Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
Hex to Binary Conversion Examples With STM32F407 Registers and C Code
Twelve worked examples: nibble diagrams, padding, hex arithmetic, register address breakdown, RGB565 packing, a hex dump tool, a hex parser and the sign-extension trap, with compiled and verified C programs.
Hex to Binary Conversion Examples and More: What This Page Covers
Theory gives you the rules; this page gives you the reps. These hex to binary conversion examples start with single bytes, grow to 32-bit values and real STM32F407 register addresses, and end with working C programs: a hex dump utility, an RGB565 colour packer and a hex-string parser. Every C program was compiled with GCC and run; the green boxes show real output.
| # | Example | Skill |
|---|---|---|
| 1 | Hex to binary with nibble diagrams | 4-bit expansion |
| 2 | Binary to hex, including padding | Grouping from the right |
| 3 | Hex to decimal with place-value diagrams | Weights of 16 |
| 4 | Decimal to hex by repeated division | Division by 16 |
| 5 | Register address breakdown: 0x40020C00 | Base + offset, bus regions |
| 6 | Hex arithmetic: carries and borrows | Offsets, region sizes |
| 7 | Byte, nibble, half-word and word diagrams | Splitting and combining |
| 8 | RGB565 colours in hex (display drivers) | Packing fields |
| 9 | A hex dump utility in C | Debug tooling |
| 10 | Parsing hex text without a library | Char to nibble |
| 11 | Sign-extension trap when printing bytes | Pitfall and fix |
| 12 | Mini exercises with hidden answers | Self-test |
Example 1: Hex to Binary With Nibble Diagrams
Rule: every hex digit becomes exactly four bits. Never skip the leading zeros of a nibble: 0x2 is 0010, not 10.
1a. A single byte: 0x2F
2 = 0010 and F = 1111, so 0x2F = 0010 1111 = 47 decimal (32 + 15).
1b. A half-word: 0xA5C3
The upper byte 0xA5 is 1010 0101, the lower byte 0xC3 is 1100 0011. Notice that byte boundaries fall exactly between hex digit pairs: a big advantage of hex.
1c. A full 32-bit word: 0xDEADBEEF
This famous “magic number” is used by debug allocators and fill patterns because it reads as a word and is easy to spot in a memory dump.
Example 2: Binary to Hex, Including Padding
Rule: group from the right, pad the left group with zeros, then replace each group.
2a. Exact multiple of four bits: 1101 0110
2b. Twelve bits: 1011 0011 1110
2c. Odd length (needs padding): 1 0110 1111 (9 bits)
| Binary | Grouped (from the right) | Hex | Decimal |
|---|---|---|---|
| 1010 | 1010 | 0xA | 10 |
| 101010 | 0010 1010 | 0x2A | 42 |
| 1111111 | 0111 1111 | 0x7F | 127 |
| 100000000 | 0001 0000 0000 | 0x100 | 256 |
| 11111111111 | 0111 1111 1111 | 0x7FF | 2047 |
Example 3: Hex to Decimal With Place-Value Diagrams
Multiply each digit by its weight (1, 16, 256, 4096 …) and add.
| Hex | Decimal | Why it matters |
|---|---|---|
| 0x7F | 127 | Largest signed 8-bit value (Lesson 2.3) |
| 0x80 | 128 | Bit 7 only |
| 0xFF | 255 | Largest byte |
| 0x3FF | 1023 | Largest 10-bit value |
| 0xFFF | 4095 | Largest 12-bit ADC value |
| 0xFFFF | 65535 | Largest 16-bit value |
| 0x7FFFFFFF | 2147483647 | Largest signed 32-bit value |
Example 4: Decimal to Hex by Repeated Division by 16
Convert 4660 to hexadecimal. Divide by 16 repeatedly; remainders 10–15 become A–F; read bottom to top.
| Step | Divide | Quotient | Remainder |
|---|---|---|---|
| 1 | 4660 ÷ 16 | 291 | 4 |
| 2 | 291 ÷ 16 | 18 | 3 |
| 3 | 18 ÷ 16 | 1 | 2 |
| 4 | 1 ÷ 16 | 0 | 1 |
Remainders from bottom to top: 1, 2, 3, 4, so 4660 = 0x1234. This matches the weights diagram in Example 3, which is the best self-check available.
Shortcut through binary for values below 256
For 200: weights give 1100 1000; regroup as C and 8: 0xC8. Quick check: 12 × 16 + 8 = 200.
| Decimal | Hex | Decimal | Hex |
|---|---|---|---|
| 10 | 0xA | 100 | 0x64 |
| 15 | 0xF | 200 | 0xC8 |
| 16 | 0x10 | 255 | 0xFF |
| 32 | 0x20 | 256 | 0x100 |
| 64 | 0x40 | 1000 | 0x3E8 |
| 128 | 0x80 | 1024 | 0x400 |
Example 5: Breaking Down a Register Address (0x40020C00)
How does the CPU “find” GPIOD? Through its address. Read 0x40020C00 from the left:
| Digits | Meaning | Calculation |
|---|---|---|
0x4000 0000 | Start of the peripheral region (APB1 begins here) | Fixed by the memory map |
+ 0x0002 0000 | AHB1 bus is 0x20000 above PERIPH_BASE | 0x40000000 + 0x20000 = 0x40020000 |
+ 0x0000 0C00 | GPIOD block: ports are 0x400 apart; D is the 4th (A,B,C,D) | 3 × 0x400 = 0xC00 |
+ 0x14 | Offset of ODR inside the port | 0x40020C00 + 0x14 = 0x40020C14 |
volatile in detail; for now remember: address + cast to volatile pointer = register.Example 6: Hex Arithmetic — Carries, Borrows and Region Sizes
Addition carries when a column reaches 16, subtraction borrows 16. Work through these:
Subtraction with borrow: 0x1000 − 0x1
The rightmost digit 0 − 1 borrows 16 from the left. The zeros ripple the borrow all the way until the leading 1 gives it: 0x1000 − 1 = 0xFFF. This is why the top address of a 4 KB block starting at 0 is 0xFFF.
| Problem | Working | Answer |
|---|---|---|
| 0x80 + 0x7F | 128 + 127 = 255 | 0xFF |
| 0x100 − 0x1 | 256 − 1 | 0xFF |
| 0xAAAA + 0x5556 | 43690 + 21846 = 65536 | 0x10000 (carry out of 16 bits) |
| 0x20000000 + 0x4000 | add to second digit group | 0x20004000 |
| 0x08004000 − 0x08000000 | end − start | 0x4000 = 16 KB sector size |
Example 7: Byte, Nibble, Half-word and Word Diagrams
A 32-bit word can be seen at four levels. Hex shows every level at once, because each level is a whole number of digits.
(w >> shift) & mask. To build: cast each part to the wide type, << it into place and OR them together. The shift amounts are always multiples of 4 (nibble) or 8 (byte), which is why hex reads so naturally.Example 8: RGB565 Colours in Hex (LCD Display Drivers)
Small TFT displays (ILI9341, ST7735) take colours as 16-bit RGB565 values: 5 bits red, 6 bits green, 5 bits blue. Hex shows the fields: white is 0xFFFF, pure red 0xF800, pure green 0x07E0 and pure blue 0x001F.
How the packing works
r & 0xF8keeps the top 5 bits of red (0xF8 = 1111 1000);<< 8moves them to bits 15..11.g & 0xFCkeeps the top 6 bits of green (0xFC = 1111 1100);<< 3puts them at bits 10..5.b >> 3keeps the top 5 bits of blue and moves them to bits 4..0.- The three parts never overlap, so OR combines them.
Example 9: A Hex Dump Utility in C
Debugging firmware often means looking at raw buffers: a received UART frame, a flash page, an I2C transaction. A hex dump prints the address, the bytes in hex and the printable characters, exactly as the xxd command or the CubeIDE memory view does.
What to notice
%08Xprints the address with eight hex digits, zero padded, so columns line up.%02Xprints every byte as exactly two digits; without the zero, 0x0A would print asAand break the layout.- Bytes outside 32–126 are not printable, so they are shown as a dot.
- The extra space after the 8th byte makes the long row easier to scan.
- On a target you would replace
printfwith your UART print function.
Example 10: Parsing Hex Text Without a Library Function
A command-line interface on a UART (“write 0x40020C14 0x1000”) must parse hex typed by a human. The key trick: shift left by 4 and OR in the new digit, the hex counterpart of the doubling method from Lesson 2.1.
Design notes
hex_valaccepts both cases:c - 'A' + 10maps A to 10, F to 15.- The
n >= 8guard prevents overflow of a 32-bit result. - Invalid input returns 0 instead of a guessed value: never silently accept garbage in a debug console.
nibble_to_charuses a 16-character string as a lookup table; masking with0xFkeeps the index in range.
Example 11: The Sign-Extension Trap When Printing Bytes in Hex
A very common bug in hex dump code: the byte is stored in a signed variable (char is signed on many platforms). When a value with bit 7 set is passed to printf, it is promoted to int with sign extension, so 0xFF becomes 0xFFFFFFFF and the output has eight digits instead of two.
uint8_t, never in plain char or int8_t, whenever you print, shift or compare them as bit patterns. Lesson 2.3 explains why bit 7 gets copied.Example 12: Mini Exercises — Try It Yourself
function revealAns(btn){var d=btn.nextElementSibling,o=d.style.display===’block’;d.style.display=o?’none’:’block’;btn.textContent=o?’Show answer’:’Hide answer’;}Quick Reference Card
| Task | Recipe |
|---|---|
| Hex → binary | Each digit → 4 bits (8-4-2-1) |
| Binary → hex | Group from the right in fours, pad left, replace |
| Hex → decimal | Sum of digit × 16position |
| Decimal → hex | Divide by 16, remainders bottom to top |
| Address of a register | Base + offset |
| Extract a byte | (w >> 8*n) & 0xFF |
| Print a register | printf("0x%08X", (unsigned)reg) |
| Print a byte | printf("%02X", (uint8_t)b) |
Examples done?
Lock in the skill with the quiz: hex conversions, address-to-peripheral matching, code snippet challenges and interview questions with full answers.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments