Hex to Decimal: A Complete Guide
Master hexadecimal-to-decimal conversion with positional notation, manual methods, and real-world programming examples.
Why Hexadecimal Matters
Hexadecimal (base 16) is the lingua franca of low-level computing. Every time you read a memory address, inspect a CSS color code, parse a MAC address, or debug a core dump, you are reading hexadecimal. The reason is elegantly simple: each hex digit maps to exactly four binary bits, making it the most compact human-readable representation of binary data. Where a single byte requires eight binary digits (10110011), hex condenses it to just two characters (B3).
Understanding how to convert between hexadecimal and decimal is a fundamental skill for software engineers, network administrators, embedded systems developers, and anyone who works close to the metal. This guide walks through the conversion process from first principles, provides practical examples, and covers the most common contexts where hex appears in everyday development.
The Hexadecimal Number System
In the decimal system you already know, each position represents a power of 10. The number 347 means 3 hundreds + 4 tens + 7 ones, or more formally: 3 × 10² + 4 × 10¹ + 7 × 10°. Hexadecimal works identically, but each position represents a power of 16 instead of 10.
Since base 16 needs 16 unique digit symbols and we only have the digits 0 through 9, hexadecimal borrows the letters A through F to represent the values 10 through 15:
The Hex Alphabet
Hexadecimal is case-insensitive: FF, ff, and Ff all represent the same value. By convention, uppercase is used in hardware documentation and memory dumps, while lowercase is common in CSS colors and URLs. Programming languages accept either.
Converting Hex to Decimal: The Positional Method
To convert a hexadecimal number to decimal, multiply each digit by 16 raised to the power of its position (counting from 0 on the right), then sum the results. This is the same positional notation process you use in decimal, just with a different base.
Let us convert 0x1A3F to decimal step by step:
- Identify digit values: 1 = 1, A = 10, 3 = 3, F = 15
- Multiply by positional weights:
- F (position 0): 15 × 16° = 15 × 1 = 15
- 3 (position 1): 3 × 16¹ = 3 × 16 = 48
- A (position 2): 10 × 16² = 10 × 256 = 2,560
- 1 (position 3): 1 × 16³ = 1 × 4,096 = 4,096
- Sum all terms: 4,096 + 2,560 + 48 + 15 = 6,719
Therefore, 0x1A3F = 6,719 in decimal. You can verify this with our Base Converter tool.
Converting Decimal to Hex: Repeated Division
To convert in the other direction, from decimal to hexadecimal, use repeated division by 16. Divide the number by 16, record the remainder (which becomes a hex digit), then repeat with the quotient until it reaches zero. Read the remainders bottom-to-top to get the hex result.
Let us convert 6,719 back to hex:
- 6,719 ÷ 16 = 419 remainder 15 (F)
- 419 ÷ 16 = 26 remainder 3 (3)
- 26 ÷ 16 = 1 remainder 10 (A)
- 1 ÷ 16 = 0 remainder 1 (1)
Reading the remainders from bottom to top: 1A3F. This confirms our earlier conversion.
Hex Prefixes and Notation
Different programming languages use different conventions to distinguish hex literals from decimal numbers:
// C, C++, Java, JavaScript, Go, Rust
int val = 0xFF; // prefix: 0x
# Python
val = 0xFF # prefix: 0x
; x86 Assembly (Intel syntax)
mov eax, 0FFh # suffix: h
// CSS
color: #FF5733; # prefix: #
// Verilog / SystemVerilog
reg [7:0] data = 8'hFF; # prefix: 'hThe 0x prefix is by far the most common. When you see it, you know the following digits are hexadecimal. Our converter supports toggling these prefixes with the "0x On/Off" button.
Where Hex Appears in Practice
Memory Addresses and Pointers
Every memory address in modern systems is displayed in hex. When your program crashes with a segfault at 0x7fff5fbff8c0, that is a 48-bit virtual address. Debuggers like GDB, LLDB, and the Chrome DevTools memory inspector all display addresses in hex because they align perfectly with page boundaries (typically 4 KB = 0x1000) and segment sizes.
CSS Colors
The ubiquitous #RRGGBB color format is three hex bytes representing red, green, and blue channel intensities from 0 to 255. For example, #38bdf8 (the accent color of this tool) breaks down to R=56 (0x38), G=189 (0xBD), B=248 (0xF8). The shorthand #RGB format doubles each digit: #F00 becomes #FF0000 (pure red). See our Color Converter for interactive exploration.
MAC Addresses and Networking
Network interface cards are identified by 48-bit MAC addresses written as six hex byte pairs: 00:1A:2B:3C:4D:5E. IPv6 addresses use eight groups of four hex digits: 2001:0db8:85a3:0000:0000:8a2e:0370:7334. Subnet masks, when expressed as hex, make their bit patterns immediately visible. Our Subnet Calculator can help with CIDR and subnet math.
File Formats and Hex Dumps
File magic numbers (the first few bytes that identify file type) are always documented in hex. JPEG files start with FF D8 FF, PNG with 89 50 4E 47, and ELF binaries with 7F 45 4C 46. Hex editors like xxd, hexdump, and HxD display file contents in hex for this reason.
Unicode and Character Encoding
Unicode code points are written in hex: U+0041 is the Latin capital letter A, U+1F600 is the grinning face emoji. Escape sequences in strings use hex in most languages: \x41 in C/Python, \u0041 in JavaScript/Java, and \U0001F600 for supplementary characters. Understanding hex makes working with character encodings, particularly UTF-8 byte sequences, far more intuitive.
Hex in Programming Languages
// JavaScript
parseInt('FF', 16) // 255 (string hex to decimal)
(255).toString(16) // 'ff' (decimal to hex string)
0xFF // 255 (hex literal)
# Python
int('FF', 16) # 255
hex(255) # '0xff'
f'{255:02X}' # 'FF' (uppercase, zero-padded)
// Go
strconv.ParseInt("FF", 16, 64) // 255
fmt.Sprintf("%X", 255) // "FF"
// Rust
i64::from_str_radix("FF", 16) // Ok(255)
format!("{:X}", 255) // "FF"Common Hex Values Worth Memorizing
Experienced developers internalize a handful of hex values that appear constantly:
0xFF= 255 (maximum unsigned byte, subnet mask octet, bitmask for lowest 8 bits)0xFFFF= 65,535 (maximum 16-bit unsigned, maximum TCP/UDP port)0x7FFFFFFF= 2,147,483,647 (maximum 32-bit signed integer)0xDEADBEEF= 3,735,928,559 (classic debug magic number)0xCAFEBABE= 3,405,691,582 (Java class file magic number)0x1000= 4,096 (common page size)
The Reference tab in our Base Converter includes a searchable table of powers of 2, byte boundaries, and common bitmasks for quick lookups.
Try It Yourself
The best way to build fluency with hex is practice. Use our Base Converter to type a value in any base and see all representations update simultaneously. Try converting your favorite color codes, memory addresses, or file magic numbers to understand the relationship between hex and decimal at a visceral level.
Further Reading
- Hexadecimal — Wikipedia
Comprehensive overview of hexadecimal history, notation, and applications.
- MDN parseInt()
JavaScript function for parsing strings in any radix, including hexadecimal.
- IEEE 754 Floating-Point
Hex representation is essential for understanding floating-point bit layouts.
- Positional Notation — Khan Academy
Interactive lessons on number systems including binary, decimal, and hexadecimal.
- Numeral System — Wikipedia
Mathematical foundations of positional numeral systems across different bases.