What Are Hexadecimal Numbers?
Hexadecimal (hex) is a base-16 number system used extensively in programming. It uses 16 distinct symbols:
- Digits: 0-9 represent values 0-9
- Letters: A-F represent values 10-15
| Hex | Decimal | Binary |
|---|---|---|
| 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 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
Why Hexadecimal?
1. Compact Representation of Binary
One hex digit = 4 binary bits (a nibble). This makes binary data much more readable:
- Binary:
11111111(8 bits) - Hex:
FF(2 digits)
2. Byte Alignment
Memory addresses, color values, and machine-level data naturally align with 8-bit bytes (2 hex digits).
3. Human-Readable
Much easier to read and write than long binary strings.
Representing Hex in Code
Different programming languages use various prefixes:
# Python hex_value = 0xFF # 255 hex_value = 0x1A # 26 print(hex(255)) # '0xff'
// JavaScript let hex = 0xFF; // 255 let hex2 = 0x1A; // 26 let str = 0xFF.toString(16); // 'ff'
// C, C++, Java, C#
int hex = 0xFF; // 255
int hex2 = 0x1A; // 26
printf("%x", 255); // 'ff'
/* CSS - Color values */ color: #FF0000; /* Red */ background: #00FF00; /* Green */ border: 1px solid #0000FF; /* Blue */
<!-- HTML Entities --> < <!-- < (less than) --> > <!-- > (greater than) -->
Common Use Cases
1. Colors (RGB/HEX)
/* RGB: Red, Green, Blue each 0-255 (00-FF) */ #FF0000 /* Red: FF=255, 00=0, 00=0 */ #00FF00 /* Green */ #0000FF /* Blue */ #FFFFFF /* White */ #000000 /* Black */ #808080 /* Gray (128,128,128) */
2. Memory Addresses
int* ptr = &some_variable;
printf("Address: %p\n", ptr); // 0x7ffeeb2c8a4c
3. Character Codes (ASCII/Unicode)
print('\x41') # 'A' (ASCII 65)
print('\u03A9') # 'Ω' (Omega)
print('\x1B[31m') # ANSI escape: red text
4. Bitmasking and Flags
#define READ_FLAG 0x01 // 0000 0001 #define WRITE_FLAG 0x02 // 0000 0010 #define EXEC_FLAG 0x04 // 0000 0100 #define ALL_FLAGS 0x07 // 0000 0111 int permissions = READ_FLAG | WRITE_FLAG; // 0x03
5. File Signatures (Magic Numbers)
# PNG file signature png_header = b'\x89PNG\r\n\x1A\n' # PDF file signature pdf_header = b'%PDF'
6. Networking (MAC Addresses, IPv6)
MAC = "00:1A:2B:3C:4D:5E" IPv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
Converting Between Number Systems
Binary to Hex
Group binary into 4-bit nibbles:
Binary: 1101 1010 ↓ ↓ Hex: D A Result: 0xDA
Decimal to Hex
# Division by 16 method 255 ÷ 16 = 15 remainder 15 (F) 15 ÷ 16 = 0 remainder 15 (F) Result: 0xFF
Programming Conversions
# Python examples
hex(255) # '0xff'
int('FF', 16) # 255
int('0xFF', 0) # 255 (auto-detect)
# Format strings
f"{255:x}" # 'ff'
f"{255:X}" # 'FF'
f"{255:#x}" # '0xff'
// JavaScript examples
parseInt("FF", 16); // 255
(255).toString(16); // 'ff'
Number("0xFF"); // 255
Common Hexadecimal Values
| Value | Hex | Binary | Common Use |
|---|---|---|---|
| 0 | 0x00 | 00000000 | Null, false |
| 255 | 0xFF | 11111111 | Max byte, white |
| 128 | 0x80 | 10000000 | Midpoint, negative flag |
| 32,768 | 0x8000 | - | 16-bit negative flag |
| 4,294,967,295 | 0xFFFFFFFF | - | Max 32-bit unsigned |
Bitwise Operations with Hex
# Bitwise operations are clearer in hex a = 0xF0 # 11110000 b = 0x0F # 00001111 print(hex(a & b)) # 0x0 (AND) print(hex(a | b)) # 0xFF (OR) print(hex(a ^ b)) # 0xFF (XOR) print(hex(~a)) # -0xf1 (NOT) print(hex(a << 1)) # 0x1E0 (left shift)
Hex Dumps
Hex dumps show raw binary data in hexadecimal format:
00000000 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 |.PNG........IHDR| 00000010 00 00 00 10 00 00 00 10 08 06 00 00 00 1F F3 FF |................|
Each column shows:
- Offset: Memory address/location
- Hex bytes: Raw data in hexadecimal
- ASCII: Human-readable representation
Best Practices
✅ Do:
- Use hex for bitmasks, flags, and low-level operations
- Use hex for color values in web development
- Use hex when working with memory addresses
- Use uppercase for readability in documentation
❌ Don't:
- Use hex for regular arithmetic (use decimal)
- Mix case inconsistently within a project
- Use hex where the base isn't relevant to the context
Key Takeaway: Hexadecimal is the bridge between human-readable decimal and machine-readable binary. It's essential for systems programming, debugging, web colors, and any situation where you need to work with bits and bytes directly.
Building Blocks of C: A Complete Guide to Functions
Explains how functions work in C programming, including function declaration, definition, parameters, return values, and how functions help organize reusable code.
https://macronepal.com/bash/building-blocks-of-c-a-complete-guide-to-functions/
The Heart of Text Processing: A Complete Guide to Strings in C
Explains how strings are used in C, covering character arrays, string handling functions, and common techniques for text processing tasks.
https://macronepal.com/bash/the-heart-of-text-processing-a-complete-guide-to-strings-in-c-2/
The Cornerstone of Data Organization: A Complete Guide to Arrays in C
Describes how arrays store multiple values in C, including indexing, initialization, and using arrays to manage structured data efficiently.
https://macronepal.com/bash/the-cornerstone-of-data-organization-a-complete-guide-to-arrays-in-c/
Guaranteed Execution: A Complete Guide to the Do-While Loop in C
Explains the do-while loop structure in C, highlighting how it ensures code runs at least once before checking the loop condition.
https://macronepal.com/bash/guaranteed-execution-a-complete-guide-to-the-do-while-loop-in-c/
Mastering Iteration: A Complete Guide to the For Loop in C
Explains how the for loop works in C, including initialization, condition checking, and increment steps for repeated execution of code blocks.
https://macronepal.com/bash/mastering-iteration-a-complete-guide-to-the-for-loop-in-c/
Mastering Iteration: A Complete Guide to While Loops in C
Explains the while loop structure in C, focusing on condition-based repetition and proper loop control techniques.
https://macronepal.com/bash/mastering-iteration-a-complete-guide-to-while-loops-in-c/
Beyond If-Else: A Complete Guide to Switch Case in C
Explains how switch-case statements work in C programming, enabling efficient handling of multiple conditional branches.
https://macronepal.com/bash/beyond-if-else-a-complete-guide-to-switch-case-in-c/
Mastering the Fundamentals: A Complete Guide to Arithmetic Operations in C
Explains how arithmetic operators such as addition, subtraction, multiplication, and division work in C, along with operator precedence and usage examples.
https://macronepal.com/bash/mastering-the-fundamentals-a-complete-guide-to-arithmetic-operations-in-c/
Foundation of C Programming: A Complete Guide to Basic Input Output
Explains how input and output functions like printf and scanf work in C, forming the foundation for interacting with users and displaying program results.
https://macronepal.com/bash/foundation-of-c-programming-a-complete-guide-to-basic-input-output/