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.
Complete C Programming Guide + Compilers Collection
1. C srand() Function â Understanding Seed Initialization
https://macronepal.com/understanding-the-c-srand-function
Explains how srand() initializes the pseudo-random number generator in C by setting a seed value. Using the same seed produces the same sequence, while time(NULL) gives different results each run.
2. C rand() Function Mechanics and Limitations
https://macronepal.com/c-rand-function-mechanics-and-limitations
Explains how rand() generates pseudo-random numbers between 0 and RAND_MAX, its deterministic nature, and limitations for security use cases.
3. C log() Function
https://macronepal.com/c-log-function-2
Covers natural logarithm calculation using <math.h> and its applications.
4. Mastering Date and Time in C
https://macronepal.com/mastering-date-and-time-in-c
Explains <time.h> functions like time(), clock(), difftime(), and struct tm.
5. Mastering time_t Type in C
https://macronepal.com/mastering-the-c-time_t-type-for-time-management
Explains time representation as seconds since Unix epoch and conversion functions.
6. C exp() Function
https://macronepal.com/c-exp-function-mechanics-and-implementation
Explains exponential function exp(x) and its scientific applications.
7. C log() Function (Alternate Guide)
https://macronepal.com/c-log-function
Comparison of log() and log10() with usage examples.
8. C log10() Function
https://macronepal.com/mastering-the-log10-function-in-c
Explains base-10 logarithm for engineering and scientific applications.
9. C tan() Function
https://macronepal.com/understanding-the-c-tan-function
Explains tangent function and radian-based calculations.
10. Random Numbers in C (Secure vs Predictable)
https://macronepal.com/mastering-c-random-numbers-for-secure-and-predictable-applications
Explains difference between rand() and secure randomness methods.
11. Free Online C Compiler
https://macronepal.com/free-online-c-code-compiler-2
Browser-based compiler for testing C programs instantly.
C Functions, Arguments, Parameters & Flow
Mastering Functions in C â Complete Guide
https://macronepal.com/c/mastering-functions-in-c-a-complete-guide/
Covers function structure, modular programming, and real-world usage.
Function Arguments in C
https://macronepal.com/c-function-arguments/
Explains how arguments are passed and used in function calls.
Function Parameters in C
https://macronepal.com/c-function-parameters/
Explains defining inputs for functions and matching them with arguments.
Function Declarations in C
https://macronepal.com/c-function-declarations-syntax-rules-and-best-practices/
Covers prototypes, syntax rules, and best practices.
Function Calls in C
https://macronepal.com/understanding-function-calls-in-c-syntax-mechanics-and-best-practices/
Explains execution flow and parameter handling during function calls.
Void Functions in C
https://macronepal.com/understanding-void-functions-in-c-syntax-patterns-and-best-practices/
Explains functions that do not return values.
Return Values in C
https://macronepal.com/c-return-values-mechanics-types-and-best-practices/
Explains different return types and how functions return results.
Pass-by-Value in C
https://macronepal.com/aws/understanding-pass-by-value-in-c-mechanics-implications-and-best-practices/
Explains how copies of variables are passed into functions.
Pass-by-Reference in C
https://macronepal.com/c/understanding-pass-by-reference-in-c-pointers-semantics-and-safe-practices/
Explains using pointers to modify original variables.
C strstr() Function
https://macronepal.com/aws/c-strstr-function/
Explains substring search inside strings in C.
C Preprocessor & Macros
https://macronepal.com/mastering-c-variadic-macros-for-flexible-debugging/
https://macronepal.com/mastering-the-stdc-macro-in-c/
https://macronepal.com/c-time-macro-mechanics-and-usage/
https://macronepal.com/understanding-the-c-date-macro/
https://macronepal.com/c-file-type/
https://macronepal.com/mastering-c-line-macro-for-debugging-and-diagnostics/
https://macronepal.com/mastering-predefined-macros-in-c/
https://macronepal.com/c-error-directive-mechanics-and-usage/
https://macronepal.com/understanding-the-c-pragma-directive/
https://macronepal.com/c-include-directive/
C Structures, Memory, Scope & Linkage
https://macronepal.com/mastering-structures-in-c/
https://macronepal.com/c-structure-declaration-mechanics-and-usage/
https://macronepal.com/c-structure-initialization-mechanics-and-best-practices/
https://macronepal.com/mastering-c-structure-member-access-for-reliable-data-handling/
https://macronepal.com/c-nested-structures/
https://macronepal.com/mastering-arrays-of-structures-in-c/
https://macronepal.com/c-structure-pointers-mechanics-and-implementation/
https://macronepal.com/understanding-c-structure-parameter-passing-mechanics/
https://macronepal.com/mastering-c-returning-structures-for-efficient-data-flow/
https://macronepal.com/c-self-referential-structures/
https://macronepal.com/mastering-structure-alignment-in-c/
https://macronepal.com/c-structure-padding-mechanics-and-optimization/
https://macronepal.com/understanding-c-flexible-array-members-mechanics-and-usage/
https://macronepal.com/mastering-c-anonymous-structures-for-flattened-data-layouts/
https://macronepal.com/c-unions/
https://macronepal.com/mastering-c-name-mangling-and-symbol-decoration/
https://macronepal.com/c-no-linkage-mechanics-and-scope-isolation/
https://macronepal.com/understanding-c-internal-linkage-mechanics-and-architecture/
C Scope, Storage Classes & Typedef
https://macronepal.com/mastering-function-prototype-scope-in-c/
https://macronepal.com/c-function-scope-mechanics-and-visibility/
https://macronepal.com/understanding-c-file-scope-mechanics-and-architecture/
https://macronepal.com/mastering-c-scope-rules-for-predictable-name-resolution/
https://macronepal.com/c-scope-rules/
https://macronepal.com/mastering-c-register-storage-class-for-historical-context-and-modern-alternatives/
https://macronepal.com/mastering-_thread_local-in-c/
https://macronepal.com/c-extern-storage-class-mechanics-and-usage/
https://macronepal.com/understanding-the-c-static-storage-class-mechanics-and-usage/
https://macronepal.com/c-auto-storage-class/
https://macronepal.com/c-typedef-with-pointers/
Extra Articles
https://macronepal.com/13757-2/
https://macronepal.com/13748-2/
https://macronepal.com/13747-2/
https://macronepal.com/13746-2/
https://macronepal.com/13745-2/
https://macronepal.com/13708-2/
https://macronepal.com/13707-2/
https://macronepal.com/13702-2/
Online Compilers
https://macronepal.com/free-html-online-code-compiler/
https://macronepal.com/free-online-python-code-compiler/
https://macronepal.com/free-online-python2-code-compiler/
https://macronepal.com/free-online-java-code-compiler/
https://macronepal.com/free-online-javascript-code-compiler/
https://macronepal.com/free-online-node-js-code-compiler/
https://macronepal.com/free-online-c-code-compiler/
https://macronepal.com/free-online-c-code-compiler-2/
https://macronepal.com/free-online-c-code-compiler-3/
https://macronepal.com/free-online-php-code-compiler/
https://macronepal.com/free-online-ruby-code-compiler/
https://macronepal.com/free-online-perl-code-compiler/
https://macronepal.com/free-online-lua-code-compiler/
https://macronepal.com/free-online-tcl-code-compiler/
https://macronepal.com/free-online-groovy-code-compiler/
https://macronepal.com/free-online-j-shell-code-compiler/
https://macronepal.com/free-online-haskell-code-compiler/
https://macronepal.com/free-online-scala-code-compiler/
https://macronepal.com/free-online-common-lisp-code-compiler/
https://macronepal.com/free-online-d-code-compiler/
https://macronepal.com/free-online-ada-code-compiler/
https://macronepal.com/free-erlang-code-compiler/
https://macronepal.com/free-online-assembly-code-compiler/
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/
