Binary to Hexadecimal Conversion: A Complete Guide with Examples
Learn how to convert between binary, hexadecimal, decimal, and octal number systems. Understand why these number bases exist and how they are used in programming, memory addressing, and hardware.
Why Do We Need Multiple Number Systems?
Humans use decimal (base 10) — ten fingers, ten digits. But computers operate on binary (base 2) — transistors that are either on or off. Hexadecimal (base 16) and octal (base 8) exist as convenient shorthand for binary: one hex digit represents exactly four binary digits, and one octal digit represents exactly three.
This makes hex and octal far more readable than raw binary when working with memory addresses, bitfields, and colour codes.
The Number Systems at a Glance
| Base | Name | Digits | Prefix |
|---|---|---|---|
| 2 | Binary | 0–1 | 0b |
| 8 | Octal | 0–7 | 0o |
| 10 | Decimal | 0–9 | (none) |
| 16 | Hexadecimal | 0–9, A–F | 0x |
Binary to Decimal
To convert binary to decimal, multiply each bit by the corresponding power of 2 and sum the results.
Binary: 1 0 1 1 0 1
Powers: 32 16 8 4 2 1
Values: 32 0 8 4 0 1 → Sum = 45
So 101101₂ = 45₁₀.
In JavaScript:
parseInt("101101", 2); // 45
Decimal to Binary
Repeatedly divide by 2 and record the remainder, then read the remainders in reverse:
45 ÷ 2 = 22 remainder 1
22 ÷ 2 = 11 remainder 0
11 ÷ 2 = 5 remainder 1
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
→ Read bottom to top: 101101
In JavaScript:
(45).toString(2); // "101101"
Binary to Hexadecimal
Group the binary digits into groups of four (padding with leading zeros if needed), then convert each group to its hex equivalent:
Binary: 1010 1111 0011
A F 3
→ 0xAF3
The hex digits for values 10–15 are A, B, C, D, E, F.
In JavaScript:
parseInt("101011110011", 2).toString(16).toUpperCase(); // "AF3"
Hexadecimal to Binary
Each hex digit expands to exactly four binary digits:
0xCAFE:
C → 1100
A → 1010
F → 1111
E → 1110
→ 1100 1010 1111 1110
Where These Conversions Are Used
Memory addresses
CPUs and debuggers display memory addresses in hex — 0x00FF1234 is far more readable than its 32-bit binary equivalent. Hex is also more compact than decimal for large addresses.
Colour codes in web design
CSS hex colours are three-byte values in hex notation: #FF5733 means R=0xFF=255, G=0x57=87, B=0x33=51. Each channel is one byte, and two hex digits represent one byte perfectly.
IPv4 and IPv6 addresses
IPv4 is four decimal octets (192.168.1.1), but IPv6 uses eight groups of four hex digits: 2001:0db8:85a3:0000:0000:8a2e:0370:7334.
File magic numbers
File formats identify themselves with "magic bytes" at the start of the file. For example, PNG files start with 89 50 4E 47 (hex) — which is \x89PNG in ASCII. Hex viewers reveal these signatures directly.
Bitmasks and flags
When storing multiple boolean flags in a single integer (common in C, embedded systems, and some database schemas), hex constants are easier to read and maintain:
#define FLAG_READ 0x01 // 0000 0001
#define FLAG_WRITE 0x02 // 0000 0010
#define FLAG_EXEC 0x04 // 0000 0100
#define FLAG_ALL 0x07 // 0000 0111
Two's Complement for Negative Numbers
In binary, negative integers are typically stored in two's complement form. To find the binary representation of a negative number:
- Take the binary representation of the absolute value
- Invert all bits (bitwise NOT)
- Add 1
-45 in 8-bit two's complement:
+45 = 0010 1101
NOT = 1101 0010
+1 = 1101 0011 → 0xD3
This is why JavaScript's ((-45) >>> 0).toString(16) gives "ffffffd3" — it converts to an unsigned 32-bit two's complement representation.