Hex / Binary Encoder ยท 6 min read
Hex, Binary, and Decimal: A Plain-English Guide to Number Systems
Binary, hexadecimal, and decimal are just different ways to represent the same numbers. Learn how each system works, why computers use binary internally, and why programmers love hex.
Positional Notation: The Key Idea
All number systems share the same underlying idea: positional notation. The value of a digit depends on its position, and each position is worth a power of the base.
In decimal (base 10), the number 347 means: 3 ร 102 + 4 ร 101 + 7 ร 100 = 300 + 40 + 7 = 347. The digits available are 0 through 9 โ ten symbols for base 10.
Every number system works the same way โ it just uses a different base and a different set of digit symbols.
Binary: Base 2
Binary uses only two symbols: 0 and 1. Each position is worth a power of 2. The binary number 1011 means: 1 ร 23 + 0 ร 22 + 1 ร 21 + 1 ร 20 = 8 + 0 + 2 + 1 = 11 in decimal.
Computers use binary because transistors โ the physical components of processors and memory โ have two stable states: on and off, high voltage and low voltage, conducting and not conducting. Mapping these two states to 1 and 0 is natural and reliable. Building reliable hardware for ten states (to support decimal) would be vastly more complex.
A single binary digit is a bit. Eight bits form a byte, which can represent 28 = 256 different values (0 through 255).
Decimal: Base 10
Decimal is the number system humans use naturally, almost certainly because we have ten fingers. It needs no further explanation โ but it is worth noting that decimal is not fundamentally superior to other bases. Its dominance is purely a product of human anatomy and historical convention.
Hexadecimal: Base 16
Hexadecimal (hex) uses sixteen symbols: 0โ9 and AโF (where A=10, B=11, C=12, D=13, E=14, F=15). Each position is worth a power of 16. The hex value FF means: 15 ร 161 + 15 ร 160 = 240 + 15 = 255 in decimal.
Why do programmers love hex? Because one hex digit exactly represents four bits, and two hex digits exactly represent one byte. This makes hex a compact, human-readable shorthand for binary data:
- Binary:
11111111(8 characters to express one byte) - Hex:
FF(2 characters to express one byte) - Decimal:
255(3 characters, and no clean relationship to bits)
Where You See Hex in the Wild
- Colors in CSS:
#FF5733โ FF = 255 red, 57 = 87 green, 33 = 51 blue (in decimal) - MAC addresses:
00:1A:2B:3C:4D:5Eโ six pairs of hex digits, one pair per byte - IPv6 addresses:
2001:0db8:85a3::8a2e:0370:7334โ groups of four hex digits - SHA-256 hashes: A 256-bit hash printed as 64 hex characters (32 bytes ร 2 hex digits each)
- Memory addresses: Debuggers display addresses as
0x7fff5fbff8b0 - UUID values:
550e8400-e29b-41d4-a716-446655440000
ASCII: Where Text Meets Binary
The ASCII standard (1963) assigned a numeric value to 128 characters โ letters, digits, punctuation, and control codes. The capital letter A is 65 in decimal, 41 in hex, and 01000001 in binary. All three refer to the same byte value.
Understanding this relationship explains why hex is used when examining file contents, network packets, or cryptographic keys: text is bytes, bytes are numbers, and hex is the most concise way to write those numbers.
UTF-8: Multi-Byte Characters
ASCII covers 128 characters โ enough for English but not for the world's scripts. UTF-8 extends ASCII by using multiple bytes for characters beyond the 127 ASCII range. The character รฉ is encoded as two bytes: 0xC3 0xA9. The Japanese character ๆฅ is three bytes: 0xE6 0x97 0xA5.
UTF-8 is backward-compatible with ASCII: any valid ASCII text is also valid UTF-8. Characters in the ASCII range (0โ127) are stored as a single byte with the same value. This compatibility is why UTF-8 became the dominant encoding for the web.
Bitwise Operations
Understanding binary also clarifies bitwise operations โ fundamental operations in cryptography, compression, and low-level programming:
- AND: Both bits must be 1.
1010 AND 1100 = 1000 - OR: At least one bit must be 1.
1010 OR 1100 = 1110 - XOR: Bits must be different.
1010 XOR 1100 = 0110โ XOR is used extensively in encryption - Left shift: Shift all bits left by n positions โ equivalent to multiplying by 2n
- Right shift: Shift all bits right โ equivalent to dividing by 2n
References
- Knuth, D. E. (1997). The Art of Computer Programming, Volume 2: Seminumerical Algorithms (3rd ed.). Addison-Wesley. (Chapter 4: Arithmetic โ positional notation systems.)
- Unicode Consortium. (2024). The Unicode Standard, Version 15.1 โ UTF-8 Encoding Form. unicode.org.
- Klensin, J. (2008). RFC 5321: Simple Mail Transfer Protocol โ hexadecimal encoding usage.
- American National Standards Institute. (1986). ANSI X3.4-1986: Coded Character Set โ 7-Bit American National Standard Code for Information Interchange (ASCII).