Binary, Decimal, and Hexadecimal: A Developer's Complete Conversion Guide
Every developer encounters binary, hexadecimal, and octal at some point. Whether you are debugging memory addresses, setting file permissions, working with colors, or preparing for a technical interview, understanding number bases is a fundamental skill. This guide covers the conversions you need to know, with practical examples from real code.
The Four Number Systems
Binary (Base 2)
Binary uses only two digits: 0 and 1. This is how computers store and process all data at the hardware level. Every piece of information - text, images, video, instructions - is ultimately represented as sequences of 0s and 1s. Each digit is called a "bit" (binary digit), and 8 bits make a "byte."
Decimal (Base 10)
Decimal is the number system humans use daily, with digits 0-9. It is called base 10 because each position represents a power of 10. The number 347 means 3x100 + 4x10 + 7x1.
Hexadecimal (Base 16)
Hexadecimal (hex) uses 16 symbols: 0-9 and A-F (where A=10, B=11, C=12, D=13, E=14, F=15). Hex is popular because each hex digit represents exactly 4 binary digits, making it a compact way to represent binary data. The prefix 0x is used in most programming languages to denote hex values.
Octal (Base 8)
Octal uses digits 0-7. It is less common today but still important for Unix/Linux file permissions. Each octal digit represents exactly 3 binary digits. The prefix 0o (or sometimes just 0) denotes octal values in code.
Quick Reference Table
| Decimal | Binary | Octal | Hexadecimal |
|---|---|---|---|
| 0 | 0000 | 0 | 0 |
| 1 | 0001 | 1 | 1 |
| 2 | 0010 | 2 | 2 |
| 3 | 0011 | 3 | 3 |
| 4 | 0100 | 4 | 4 |
| 5 | 0101 | 5 | 5 |
| 6 | 0110 | 6 | 6 |
| 7 | 0111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 255 | 11111111 | 377 | FF |
How to Convert: Step by Step
Binary to Decimal
Multiply each digit by its positional power of 2, then add them up. Positions count from right to left, starting at 0:
Example: 1101 in binary
- 1 x 2^3 = 8
- 1 x 2^2 = 4
- 0 x 2^1 = 0
- 1 x 2^0 = 1
- Total: 8 + 4 + 0 + 1 = 13 in decimal
Decimal to Binary
Repeatedly divide by 2 and record the remainders. Read the remainders from bottom to top:
Example: 25 in decimal
- 25 / 2 = 12 remainder 1
- 12 / 2 = 6 remainder 0
- 6 / 2 = 3 remainder 0
- 3 / 2 = 1 remainder 1
- 1 / 2 = 0 remainder 1
- Reading bottom to top: 11001 in binary
Hex to Binary (and Back)
This is the easiest conversion because each hex digit maps to exactly 4 binary digits. Just substitute each hex digit with its 4-bit binary equivalent:
Example: 0x2F in hex
- 2 = 0010
- F = 1111
- Result: 00101111 in binary
To go from binary to hex, group the binary digits into sets of 4 (from right to left, padding with zeros if needed) and convert each group:
Example: 11010110 in binary
- 1101 = D
- 0110 = 6
- Result: 0xD6 in hex
Decimal to Hex
Repeatedly divide by 16 and record the remainders (using A-F for values 10-15). Read remainders from bottom to top:
Example: 255 in decimal
- 255 / 16 = 15 remainder 15 (F)
- 15 / 16 = 0 remainder 15 (F)
- Result: 0xFF in hex
Skip the manual math and use our number base converter for instant, error-free conversions between any base.
Where Each Base Is Used in Real Code
Hexadecimal in CSS Colors
CSS hex colors like #FF5733 are three hex byte values: red (FF = 255), green (57 = 87), blue (33 = 51). Each pair of hex digits represents an 8-bit color channel with values from 0 to 255. The shorthand #F00 expands to #FF0000 (pure red).
Octal in Unix File Permissions
The command chmod 755 uses octal. Each digit represents 3 permission bits: read (4), write (2), execute (1). So 7 = rwx (4+2+1), 5 = r-x (4+0+1). The three digits represent owner, group, and others respectively.
Binary in Networking
IP addresses are 32-bit binary numbers. The address 192.168.1.1 is actually 11000000.10101000.00000001.00000001 in binary. Subnet masks use binary to determine which bits represent the network vs. the host.
Hexadecimal in Memory Addresses
Memory addresses are displayed in hex because they are compact and align with byte boundaries. An address like 0x7FFE4200 is much easier to read than its binary equivalent of 32 ones and zeros.
Two's Complement: Negative Numbers in Binary
Computers represent negative integers using two's complement. To find the two's complement of a number:
- Write the number in binary
- Flip all the bits (0 becomes 1, 1 becomes 0)
- Add 1
Example: -5 in 8-bit two's complement
- 5 in binary: 00000101
- Flip bits: 11111010
- Add 1: 11111011
In two's complement, the leftmost bit indicates the sign: 0 for positive, 1 for negative. An 8-bit two's complement number can represent values from -128 to 127. This system is elegant because addition and subtraction work the same way for both positive and negative numbers, simplifying hardware design.
Common Bit Values to Memorize
| Power of 2 | Decimal Value | Common Use |
|---|---|---|
| 2^8 | 256 | One byte range (0-255) |
| 2^10 | 1,024 | 1 KB (kibibyte) |
| 2^16 | 65,536 | Port number range |
| 2^20 | 1,048,576 | 1 MB (mebibyte) |
| 2^32 | 4,294,967,296 | IPv4 address space, 32-bit integer max |
| 2^64 | ~1.8 x 10^19 | 64-bit integer max |
Frequently Asked Questions
Why do computers use binary instead of decimal?
Computers use binary because transistors (the fundamental building blocks of processors) have two states: on and off, which naturally map to 1 and 0. Building reliable hardware that distinguishes between just two voltage levels is far simpler and more error-resistant than hardware that must distinguish between 10 levels (for decimal). This simplicity at the hardware level is why binary became the universal language of computing.
What is the difference between 0x and # notation for hex?
The 0x prefix is used in programming languages (JavaScript, Python, C, Java) to indicate a hexadecimal literal, e.g., 0xFF. The # prefix is specifically used in CSS for color codes, e.g., #FF5733. Both represent the same hex values, just in different contexts. Some languages also use \x for hex escape sequences in strings.
How do I convert hex colors to RGB values?
Split the 6-digit hex color into three pairs and convert each pair from hex to decimal. For #FF5733: FF = 255 (red), 57 = 87 (green), 33 = 51 (blue), giving you RGB(255, 87, 51). Use our number base converter for the hex-to-decimal conversion of each pair.
What is the largest number a byte can store?
A byte is 8 bits. For unsigned integers, the range is 0 to 255 (2^8 - 1), which is FF in hex. For signed integers using two's complement, the range is -128 to 127. This is why RGB color values go from 0 to 255, and why many early video games had stats capped at 255.
Why is hexadecimal used instead of just showing binary?
Hex is a compact representation of binary. One hex digit represents exactly 4 bits, so a 32-bit number that would be 32 characters in binary is just 8 characters in hex (e.g., 0xDEADBEEF vs 11011110101011011011111011101111). Hex is easier for humans to read, compare, and remember while maintaining a direct and simple mapping to binary.