Hexadecimal Number System in Embedded C: Complete Guide-Free Embedded C Course in Hyderabad

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 2 · LESSON 2.2 · THEORY

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.

45 min
Read Time
Beginner
Level
M2 · L2
Module / Lesson

What You Will Learn

✓ Why hex is the standard notation of datasheets and debuggers
✓ Hex digits 0–9 and A–F and base-16 place values
✓ Hex to binary with 4-bit grouping, and back
✓ Hex to decimal and decimal to hex conversion
✓ The 0x prefix, literal types and printf formats in C
✓ Reading STM32F407 addresses and register offsets in hex
✓ Hex masks, nibble and byte extraction
✓ Hex dumps, memory browser and little-endian byte order
✓ Hex addition for address and offset calculation
✓ Common mistakes and best practices

Key Terms

Hexadecimal (base-16)Nibble0x prefixHex digit A–F4-bit groupingBase addressRegister offsetBit maskLittle-endianHex dump%x / %X / %08XMemory-mapped I/O

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:

One 32-bit value: binary versus hexadecimal
Binary
0
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
1
1
0
0
0
0
0
1
1
0
0
0
0
Hex
4
0
0
2
3
8
3
0

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:

RepresentationDigits for a 32-bit valueMaps to bits cleanly?Typical use
Binary (base 2)32Yes, 1 digit = 1 bitBit-level reasoning, short fields
Octal (base 8)11No, 3 bits per digit do not divide 8/16/32Unix file permissions
Decimal (base 10)10No, no relation to bit boundariesHuman quantities: 100 ms, 9600 baud
Hexadecimal (base 16)8Yes, 1 digit = 4 bits (a nibble)Addresses, registers, masks, memory dumps
Rule of thumb: Use decimal for quantities that humans reason about (timeouts, baud rates, array sizes), hexadecimal for anything that is a bit pattern, an address or a register value, and binary only for short fields where you need to see individual bits.

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.

DecimalHex digit4-bit binaryNote
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
10A1010ten
11B1011eleven
12C1100twelve
13D1101thirteen
14E1110fourteen
15F1111fifteen

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:

0x3E8 = 3×256 + 14×16 + 8×1
weight
digit
digit × weight
162
256
3
768
161
16
E
224
160
1
8
8
3 × 256 + 14 × 16 + 8 × 1 = 1000
Memory aid: The sixteen weights you need most: 160 = 1, 161 = 16, 162 = 256, 163 = 4,096, 164 = 65,536. Note that 162 = 28 (one byte = two hex digits) and 168 = 232 (one 32-bit word = eight hex digits).

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

Each hex digit expands to a nibble: B = 1011, 5 = 0101
B
▼
1
0
1
1
= 11
5
▼
0
1
0
1
= 5

Result: 0xB5 = 1011 0101 (= 181 decimal). The digit B is 11, so its nibble is 8 + 2 + 1 = 1011.

Example: 0x3F8A → binary

Four hex digits become sixteen bits
3
▼
0
0
1
1
= 3
F
▼
1
1
1
1
= 15
8
▼
1
0
0
0
= 8
A
▼
1
0
1
0
= 10

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:

Grouping 1011010 into nibbles from the right (pad the left group)
1011010
original, 7 bits
➜
Pad left
0101 1010
➜
Group
0101 | 1010
➜
Replace
5 and A
➜
Result
0x5A
Grouping from the wrong side: If you group from the left you get 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

Hex digit ↔ nibble reference
0
0000
0
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
8
9
1001
9
A
1010
10
B
1011
11
C
1100
12
D
1101
13
E
1110
14
F
1111
15

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

0x1A3F in decimal
weight
digit
digit × weight
163
4,096
1
4096
162
256
A
2560
161
16
3
48
160
1
F
15
1 × 4096 + 10 × 256 + 3 × 16 + 15 × 1 = 6719
HexCalculationDecimalWhere it appears
0xFF15×16 + 15255Largest byte
0x3E83×256 + 14×16 + 81000Timer count for 1 ms at 1 MHz (+1)
0x1A3F1×4096 + 10×256 + 3×16 + 156719Arbitrary 16-bit value
0x80008×409632768Bit 15 only
0xFFFF15×4096 + 15×256 + 15×16 + 1565535Largest 16-bit value
0x0800 00008×166134217728Start 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:

Division table for 6719 (base 16)
StepDivideQuotientRemainder
16719 ÷ 16419F
2419 ÷ 16263
326 ÷ 161A
41 ÷ 1601
▲ Read the remainders from the last step up to the first step (the chips show hex digits) (bottom to top)

Reading the remainders from bottom to top gives 1 A 3 F, so 6719 = 0x1A3F, which matches the previous example in reverse.

Fast route for firmware engineers: Do not divide by 16 for large values. Convert decimal to binary with the weights (Lesson 2.1) and regroup into nibbles. Or let the compiler do it: write 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).

NotationExampleValueComment
Decimal255255No prefix
Hexadecimal0xFF, 0XFF, 0xff255Prefix 0x; digits are case-insensitive
Octal0377255Leading zero only. Avoid!
Binary (extension / C23)0b11111111255GCC extension; official in C23
Unsigned suffix0xFFu, 0xFFFFFFFFUL255 / 4294967295U = 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.

literal_types.c — _Generic reveals the type of each constant
#include <stdio.h> #include <stdint.h> #define TYPE_NAME(x) _Generic((x), \ int: “int”, unsigned int: “unsigned int”, \ long: “long”, unsigned long: “unsigned long”, \ long long: “long long”, unsigned long long: “unsigned long long”, \ default: “other”) int main(void) { printf(“0x7FFFFFFF is %s\n”, TYPE_NAME(0x7FFFFFFF)); printf(“0x80000000 is %s\n”, TYPE_NAME(0x80000000)); printf(“0xFFFFFFFF is %s\n”, TYPE_NAME(0xFFFFFFFF)); printf(“0xFFu is %s\n”, TYPE_NAME(0xFFu)); printf(“0x100000000 is %s\n”, TYPE_NAME(0x100000000)); return 0; }
Output on a 64-bit PC (verified)
0x7FFFFFFF is int 0x80000000 is unsigned int 0xFFFFFFFF is unsigned int 0xFFu is unsigned int 0x100000000 is long
On the STM32F407 (ARM Cortex-M4, 32-bit 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

hex_printf.c
#include <stdio.h> #include <stdint.h> int main(void) { uint32_t reg = 0x00ABCDEFu; uint8_t b = 0x0A; printf(“%%x -> %x\n”, (unsigned)reg); /* lower case, no padding */ printf(“%%X -> %X\n”, (unsigned)reg); /* upper case */ printf(“%%#X -> %#X\n”, (unsigned)reg); /* adds 0X prefix */ printf(“%%08X -> %08X\n”, (unsigned)reg); /* 8 digits, zero padded */ printf(“%%02X -> %02X\n”, (unsigned)b); /* one byte, 2 digits */ printf(“%%d -> %d\n”, (int)reg); /* same value in decimal */ printf(“0x%%08X-style: 0x%08X\n”, (unsigned)0x40020C14u); return 0; }
Output (verified)
%x -> abcdef %X -> ABCDEF %#X -> 0XABCDEF %08X -> 00ABCDEF %02X -> 0A %d -> 11259375 0x%08X-style: 0x40020C14
FormatMeaning
%x / %XHex, lower / upper case, no prefix, no padding
%#xAdds the 0x prefix (but not for zero)
%08XEight digits, zero padded: the standard way to print a 32-bit register
%02XTwo digits: a single byte in a hex dump
%pPointer value; prints an address (format is implementation defined)
%lX, %llXFor long and long long arguments

Parsing hex text

hex_parse.c — strtoul with base 16 and base 0
#include <stdio.h> #include <stdlib.h> int main(void) { const char *s = “3E8”; unsigned long v = strtoul(s, NULL, 16); /* parse text as base 16 */ unsigned long w = strtoul(“0x40020C00”, NULL, 0); /* base 0: honours 0x / 0 prefix */ printf(“\”%s\” -> %lu\n”, s, v); printf(“\”0x40020C00\” -> %lu = 0x%lX\n”, w, w); return 0; }
Output (verified)
“3E8” -> 1000 “0x40020C00” -> 1073875968 = 0x40020C00

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.

STM32F407 memory map (simplified, high addresses on top)
0xE000 0000
Cortex-M4 internal peripheralsSysTick, NVIC, SCB, debug
0x5000 0000
AHB2 peripheralsUSB OTG FS, DCMI, RNG
0x4002 0000
AHB1 peripheralsGPIOA…I, RCC, DMA, Flash interface
0x4001 0000
APB2 peripheralsUSART1, ADC, SPI1, TIM1
0x4000 0000
APB1 peripheralsTIM2…7, USART2, I2C1
0x2002 0000
(end of SRAM — initial stack pointer)
0x2001 C000
SRAM2 — 16 KB
0x2000 0000
SRAM1 — 112 KB
0x1000 0000
CCM data RAM — 64 KBCPU only, no DMA
0x0800 0000
Main Flash — 1 MBcode and constants, ends at 0x080F FFFF

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:

0x40020C14 = 0x40020C00 (GPIOD base) + 0x14 (ODR offset)
4
0
0
2
0
C
1
4
0x4002 → peripheral region, AHB1 bus
0x0C → block of GPIOD (0x0C00 ÷ 0x400 = 3 = port D)
0x14 → ODR

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:

RegisterOffsetAddress (GPIOD)Purpose
GPIOD_MODER0x000x40020C00Mode (input / output / AF / analog)
GPIOD_OTYPER0x040x40020C04Output type (push-pull / open-drain)
GPIOD_OSPEEDR0x080x40020C08Output speed
GPIOD_PUPDR0x0C0x40020C0CPull-up / pull-down
GPIOD_IDR0x100x40020C10Input data (read pins)
GPIOD_ODR0x140x40020C14Output data (drive pins)
GPIOD_BSRR0x180x40020C18Bit set / reset (atomic)
GPIOD_LCKR0x1C0x40020C1CConfiguration lock
GPIOD_AFRL0x200x40020C20Alternate function, pins 0-7
GPIOD_AFRH0x240x40020C24Alternate function, pins 8-15

Common peripheral base addresses

PeripheralBase addressBus
TIM20x40000000APB1
USART20x40004400APB1
I2C10x40005400APB1
USART10x40011000APB2
ADC10x40012000APB2
SPI10x40013000APB2
GPIOA0x40020000AHB1
GPIOB0x40020400AHB1
GPIOC0x40020800AHB1
GPIOD0x40020C00AHB1
GPIOE0x40021000AHB1
RCC0x40023800AHB1
Where do these numbers come from? From the Memory map chapter of the STM32F405/407 reference manual (RM0090) and the CMSIS device header, which defines macros such as 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.

MaskBinary patternSelects
0x0F0000 1111Low nibble of a byte
0xF01111 0000High nibble of a byte
0xFF1111 1111One byte
0xFF001111 1111 0000 0000Byte 1 of a 16/32-bit value
0x0000FFFF16 onesLower half-word
0x800000001 followed by 31 zerosBit 31 (MSB of a 32-bit word)
0x0000000131 zeros then 1Bit 0 (LSB)

How AND with a mask works

0xB5 & 0xF0 keeps the high nibble and clears the low nibble
value 0xB5
7
1
6
0
5
1
4
1
3
0
2
1
1
0
0
1
mask 0xF0
1
1
1
1
0
0
0
0
result 0xB0
1
0
1
1
0
0
0
0
hex_masks.c — nibble and byte extraction on a 32-bit value
#include <stdio.h> #include <stdint.h> int main(void) { uint32_t reg = 0xB5C3A1F0u; uint32_t low_nibble = reg & 0x0000000Fu; /* bits 3..0 */ uint32_t byte1 = (reg >> 8) & 0xFFu; /* bits 15..8 */ uint32_t top_nibble = (reg >> 28) & 0xFu; /* bits 31..28 */ uint32_t cleared = reg & ~0x0000FF00u; /* force byte 1 to zero */ uint32_t forced = reg | 0x0000000Fu; /* force low nibble to F */ printf(“reg = 0x%08X\n”, (unsigned)reg); printf(“low nibble = 0x%X\n”, (unsigned)low_nibble); printf(“byte 1 = 0x%02X\n”, (unsigned)byte1); printf(“top nibble = 0x%X\n”, (unsigned)top_nibble); printf(“byte1 = 0 : 0x%08X\n”, (unsigned)cleared); printf(“low = F : 0x%08X\n”, (unsigned)forced); return 0; }
Output (verified)
reg = 0xB5C3A1F0 low nibble = 0x0 byte 1 = 0xA1 top nibble = 0xB byte1 = 0 : 0xB5C300F0 low = F : 0xB5C3A1FF

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:

uint32_t word = 0x12345678 stored little-endian at 0x20000000
Memory as the debugger shows it (byte by byte, rising addresses)
0x20000000
78
0x20000001
56
0x20000002
34
0x20000003
12
LSB 0x78 at the lowest addressMSB 0x12 at the highest address
endian_dump.c — view a 32-bit word as four bytes
#include <stdio.h> #include <stdint.h> int main(void) { uint32_t word = 0x12345678u; const uint8_t *p = (const uint8_t *)&word; /* view the same memory as bytes */ for (int i = 0; i < 4; i++) { printf(“byte at offset +%d = 0x%02X\n”, i, p[i]); } return 0; }
Output on a little-endian machine (verified; identical on Cortex-M4)
byte at offset +0 = 0x78 byte at offset +1 = 0x56 byte at offset +2 = 0x34 byte at offset +3 = 0x12
Reading dumps correctly: The memory window shows bytes, so you read 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.

0x2F + 0x1B = 0x4A (F + B = 15 + 11 = 26 = 0x1A: write A, carry 1)
carry
1
A
2
F
+ B
1
B
sum
4
A
0x40020C00 + 0x14 = 0x40020C14
carry
A
4
0
0
2
0
C
0
0
+ B
0
0
0
0
0
0
1
4
sum
4
0
0
2
0
C
1
4

Useful region calculations

QuestionCalculationAnswer
Last Flash address (1 MB)0x08000000 + 0x100000 − 10x080FFFFF
End of SRAM1 + SRAM2 (128 KB = 0x20000)0x20000000 + 0x200000x20020000 (initial stack pointer)
Address of GPIOC0x40020000 + 2 × 0x4000x40020800
Size of region 0x20000000 – 0x2001BFFF0x1C000 = 114,688112 KB (SRAM1)
Address of the 5th 32-bit element of an array at 0x200001000x20000100 + 5 × 40x20000114
Check yourself with C: The compiler is the best hex calculator: 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.

Click the bits of a 16-bit register
15
0
32768
14
0
16384
13
0
8192
12
0
4096
11
0
2048
10
0
1024
9
0
512
8
0
256
7
0
128
6
0
64
5
0
32
4
0
16
3
0
8
2
0
4
1
0
2
0
0
1
Unsigned decimal
0
Signed (2’s complement)
0
Hexadecimal
0
Binary
0
(function(){var N=16,v=0; function r(){var u=v>>>0,s=(N>(N-1))&1)?v-Math.pow(2,N):v,b=”,h; for(var i=N-1;i>=0;i–){var on=(v>>>i)&1,e=document.getElementById(‘t16_b’+i);b+=on;e.textContent=on; e.style.background=on?’#10b981′:’#e2e8f0′;e.style.color=on?’white’:’#475569′;e.style.borderColor=on?’#10b981′:’#cbd5e1′;if(i%4==0&&i>0)b+=’ ‘;} h=u.toString(16).toUpperCase();while(h.length<N/4)h=’0’+h; document.getElementById(‘t16_dec’).textContent=u;document.getElementById(‘t16_sgn’).textContent=s; document.getElementById(‘t16_hex’).textContent=’0x’+h;document.getElementById(‘t16_bin’).textContent=b;} window.tgl_t16=function(i){v=(v^(1<>>0;if(N<32)v=v&((1<<N)-1);r();}; window.rst_t16=function(){v=0;r();}; window.ful_t16=function(){v=(N<32)?((1<<N)-1):0xFFFFFFFF;r();}; r();})();

11. Real-World Uses of the Hexadecimal Number System in Embedded C

Use caseExampleWhy hex?
Register addresses0x40023830 (RCC_AHB1ENR)Datasheet notation; shows bus and block structure
Bit masksGPIOD->ODR &= ~0xF000;Four F digits = four bits; readable mask
Device I2C addressesMPU6050 at 0x68, BMP280 at 0x76Vendors publish 7-bit addresses in hex
Protocol bytesCAN ID 0x7DF, command 0xA5Frames are specified as hex byte sequences
Colours (LCD)RGB565 white = 0xFFFFFixed-width fields
Magic numbers0xDEADBEEF, 0xA5A5A5A5Easy to spot in a memory dump (stack fill, error marker)
Linker script and map filesORIGIN = 0x08000000, LENGTH = 1024KAddresses are always hex
Checksums and CRCCRC-32 result 0xCBF43926Compact representation of 32 bits

12. Common Mistakes With Hexadecimal in C

Mistake 1 — forgetting the 0x prefix: int mask = FF; is a compile error, but int addr = 4002; compiles and gives decimal 4002, not hex 0x4002. Always prefix.
Mistake 2 — confusing the digit weight: Treating 0x10 as ten. It is sixteen. A hex digit in the second column is worth 16, not 10.
Mistake 3 — wrong printf specifier: printf("%d", 0xFFFFFFFF) prints −1 for a 32-bit int. Use %u or %X and an unsigned type.
Mistake 4 — misaligned nibble grouping: Grouping binary from the left instead of the right. Always group from the LSB.
Mistake 5 — mixing 0 and O: In hex digits the letter O does not exist. 0xFOO is a syntax error; the zero is 0.
Mistake 6 — wrong-width masks: ~0xFF on a 16-bit value does not give 0xFF00 but 0xFFFFFF00 after integer promotion. Mask the result: (~0xFFu) & 0xFFFFu.
Mistake 7 — reading little-endian dumps as big-endian: The bytes 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: %08X for 32-bit, %04X for 16-bit, %02X for 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, 0x marks hex, a lone leading 0 means 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

What is the hexadecimal number system in embedded C?
It is the base-16 notation in which firmware writes addresses, register values and bit masks. Each hex digit (0–9, A–F) represents four bits, so a byte is two digits and a 32-bit register is eight. In C you write it with the 0x prefix, for example 0x40020C00.
Why do embedded engineers prefer hex over binary?
Binary strings of 16 or 32 digits are hard to read, copy and compare. Hex is a lossless, compact form: converting between the two needs no arithmetic, only a 16-entry table of 4-bit patterns.
How do I convert hex to binary quickly?
Replace each hex digit with its 4-bit value (8-4-2-1 weights). Example: 0xB5 = 1011 0101, because B = 1011 and 5 = 0101.
How do I convert hex to decimal?
Multiply each digit by 16 raised to its position and add: 0x3E8 = 3×256 + 14×16 + 8 = 1000.
What does 0x mean in C and what is the difference to a leading 0?
0x starts a hexadecimal constant. A leading 0 without x starts an octal constant, so 010 equals 8 and 0x10 equals 16.
How do I print a register value in hex with leading zeros?
Use printf("0x%08X\n", (unsigned)reg); for 32-bit values, %04X for 16-bit and %02X for bytes.
Why does the STM32F407 GPIOD ODR sit at 0x40020C14?
GPIOD starts at base address 0x40020C00 (each GPIO port block is 0x400 bytes, port D is the fourth) and the ODR register has offset 0x14, so the address is 0x40020C00 + 0x14.
What does little-endian mean for hex dumps?
The least significant byte is stored at the lowest address. The 32-bit value 0x12345678 appears in memory as the byte sequence 78 56 34 12.
Is 0xFFFFFFFF signed or unsigned in C?
It does not fit in a 32-bit int, so its type is unsigned int (hex literals may take an unsigned type automatically). Storing it in an int variable gives −1 on two’s complement machines.

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.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *