Number Base Conversion: The Developer's Practical Guide
If you've ever stared at a hex color code like #FF6B6B and wondered what those letters mean, or tried to read a network packet dump full of 0x prefixes, you've already bumped into number base conversion. This guide explains how binary, octal, decimal, and hexadecimal relate to each other β and gives you the tools to move between them confidently.
Why Different Number Bases Exist
Humans count in base 10 (decimal) because we have ten fingers. Computers count in base 2 (binary) because their transistors are either on or off. Hex (base 16) exists because it's a compact way to represent binary data β four binary digits collapse into one hex digit. Octal (base 8) was popular in early Unix systems and still appears in file permissions.
Understanding base conversion is essential for:
- Reading and writing memory addresses
- Debugging network protocols
- Working with color values in CSS and design tools
- Understanding file permissions on Unix-like systems
- Interpreting binary file formats and encodings
Why Computers Use Binary
The reason computers count in base 2 is physical, not mathematical. Every piece of data inside a computer is ultimately stored or transmitted as electrical signals, and the simplest reliable state for a circuit to hold is one of two values: on or off, high voltage or low voltage, charged or uncharged. That two-state component is a transistor.
A modern CPU contains billions of transistors. Each one holds exactly one bit of information β a 0 or a 1. Group eight of them together and you have a byte, capable of holding 256 different values (2βΈ). There is no technical reason why a transistor could not represent three or more states, and some research architectures have explored this, but two-state logic is dramatically more reliable. The gap between "on" and "off" is easy to detect even when voltage fluctuates slightly; distinguishing ten precise voltage levels reliably at billions of operations per second would be far harder.
Everything your computer does β arithmetic, text, images, video, network packets β is ultimately a stream of 0s and 1s being switched through transistors at enormous speed. Binary is not a convention that engineers chose for tidiness. It is the direct consequence of the hardware.
This also explains why binary arithmetic is so central to programming:
- Bit shifting (
<<and>>operators) multiplies or divides by powers of 2 in a single CPU instruction, far faster than general multiplication. - Bitwise AND, OR, XOR manipulate individual bits directly, with no overhead.
- Memory alignment rules in C and other low-level languages exist because the hardware reads data most efficiently at addresses that are multiples of 2, 4, or 8.
Once you accept that hardware is binary, every other base used in computing β hex, octal β makes sense as a human convenience for reading and writing binary data.
How Hexadecimal Maps to Binary Nibbles
The relationship between hex and binary is the single most useful thing to understand about hexadecimal. Once it clicks, you will read hex values instinctively.
Four binary digits β a nibble β can represent exactly 16 values (2β΄ = 16). Hexadecimal has exactly 16 symbols: 0β9 and AβF. This means every possible nibble maps to exactly one hex digit, with no remainder and no ambiguity.
A full byte (8 bits) is two nibbles, so it maps to exactly two hex digits. This is why byte values always fit in two hex characters, and why hex is ubiquitous anywhere you deal with raw bytes:
Binary: 1111 1111 β Hex: FF β Decimal: 255
Binary: 1010 0011 β Hex: A3 β Decimal: 163
Binary: 0000 0001 β Hex: 01 β Decimal: 1
The conversion requires no arithmetic β you look up each group of four bits in the table above and write down the corresponding hex digit. That is the whole operation. Compare this to converting binary to decimal, which requires multiplying out powers of 2 and summing them. Hex is simply easier to read and write when you are working close to the hardware.
The Core Example: Decimal 42
Let's use the number 42 to show how the same value looks in each base.
Binary (101010): Each digit is a power of 2. Reading right to left: 2 + 8 + 32 = 42.
Octal (52): Each digit is a power of 8. (5 Γ 8) + (2 Γ 1) = 42.
Hex (2A): Each digit is a power of 16. (2 Γ 16) + (10 Γ 1) = 42. The letter A represents 10.
Converting by Hand
Decimal to Binary
Divide by 2 repeatedly and record the remainders from bottom to top:
42 Γ· 2 = 21 remainder 0
21 Γ· 2 = 10 remainder 1
10 Γ· 2 = 5 remainder 0
5 Γ· 2 = 2 remainder 1
2 Γ· 2 = 1 remainder 0
1 Γ· 2 = 0 remainder 1
Read remainders upward: 101010
Decimal to Hex
Divide by 16 and map remainders to hex digits (10=A, 11=B ... 15=F):
42 Γ· 16 = 2 remainder 10 (A)
2 Γ· 16 = 0 remainder 2
Read upward: 2A
Binary to Hex (the shortcut)
Group binary digits into sets of four from the right, then convert each group:
101010 β 0010 1010
2 A
Result: 2A β this is why hex is so useful for compressing binary data.
Real-World Example 1: IP Addresses in Hex
Network engineers and protocol developers often see IP addresses in hexadecimal. The IPv4 address 192.168.1.1 breaks down like this:
192 β C0
168 β A8
1 β 01
1 β 01
Hex representation: C0A8 0101
This is exactly how IP addresses appear in raw packet captures and network debugging tools. If you've used Wireshark, you've seen this format.
Real-World Example 2: CSS Color Codes
The color #FF6B6B is a hex triplet. Each pair of digits represents one color channel (0β255):
FF β 255 (Red)
6B β 107 (Green)
6B β 107 (Blue)
So #FF6B6B is RGB(255, 107, 107) β a coral red. When designers say "full red channel," they mean FF. When they say "no blue," they mean 00. Hex color codes are just three base-16 numbers written back to back.
Converting 6B to decimal: (6 Γ 16) + 11 = 107.
File Permissions and Octal
Unix file permissions like chmod 755 use octal directly:
7 = 111 in binary β read + write + execute
5 = 101 in binary β read + execute (no write)
5 = 101 in binary β read + execute (no write)
So 755 means owner can do everything, group and others can read and execute. Octal maps cleanly onto three-bit permission groups β that's why Unix chose it.
Real-World Uses of Each Base
Understanding where each base actually appears in practice turns abstract knowledge into a usable skill.
Binary: Bit Flags and Bitmasks
Binary is most useful to programmers not for representing large numbers but for packing multiple boolean values into a single integer. This technique is called a bitmask.
Consider Unix-style file permissions at the bit level. Each permission β read, write, execute β is one bit. By OR-ing them together you can represent any combination in a single byte:
Read = 0b100 (4)
Write = 0b010 (2)
Execute = 0b001 (1)
Read + Write = 0b110 (6)
Read + Execute = 0b101 (5)
Read + Write + Exec = 0b111 (7)
To check whether the read bit is set you use a bitwise AND:
permissions & 0b100 β non-zero means read is allowed
This pattern appears throughout systems programming and protocol design. HTTP/2 frame flags, TCP header flags (SYN, ACK, FIN), CSS contain values, and game engine entity flags all use bitmasks because they are compact and fast.
Octal: Unix File Permissions (chmod 755)
chmod 755 is the most common octal value a developer types. Here is what each digit means:
7 β 111 β read (4) + write (2) + execute (1) β owner
5 β 101 β read (4) + execute (1) β group
5 β 101 β read (4) + execute (1) β others
So rwxr-xr-x in symbolic notation is 755 in octal. A web server directory typically uses 755 (public can enter and read, only the owner can write). A configuration file with secrets uses 600 (owner can read and write, nobody else can do anything). A shell script that needs to run uses 744 or 755 depending on whether you want others to execute it.
The reason octal fits perfectly is that each octal digit represents exactly three bits, and Unix stores permissions as three groups of three bits. Octal is not used elsewhere as much anymore, but for file permissions it remains the standard.
Hexadecimal: Memory, Colours, MAC Addresses, and Hashes
Hex appears constantly at the boundary between human-readable text and raw binary data.
Memory addresses: debuggers and crash dumps print addresses as hex β for example 0x7ffd3a2b10c0. The 0x prefix is the conventional way to signal "this is hex". A 64-bit pointer is 8 bytes, which is 16 hex digits.
Colour codes: #FF5733 is a 24-bit RGB value. FF is the red channel (255), 57 is green (87), 33 is blue (51). Designers rarely think in decimal for colours; hex triplets map directly to byte values and are standard across CSS, design tools, and SVG.
MAC addresses: a network interface's hardware address is 6 bytes written as six pairs of hex digits β for example 3C:22:FB:0A:B1:7E. Each pair is one byte, 0 to 255.
SHA hashes: a SHA-256 hash is 32 bytes displayed as 64 hex characters. When you run sha256sum on a file and see e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855, each pair of characters is one byte of the hash output.
Decimal: Human Communication
Decimal exists in programming primarily at the human interface layer β user-facing numbers, business logic, prices, counts. Internally, the computer works in binary. When you write 42 in source code, the compiler converts it to binary. When the result is displayed on screen, the runtime converts it back to decimal. The decimal representation you see is a translation for your benefit.
Overflow and Integer Limits
Every integer type in a programming language is stored as a fixed number of bits, and that imposes a hard ceiling on the values it can hold. When a calculation pushes a number past that ceiling, you get overflow β one of the most common sources of subtle bugs in systems programming.
A signed 8-bit integer (int8) has 8 bits. One bit is used for the sign (positive or negative), leaving 7 bits for magnitude. Its range is β128 to 127. If you add 1 to 127 in an int8:
0111 1111 (127)
+0000 0001 (1)
ββββββββββ
1000 0000 (-128 in two's complement)
The result wraps around to β128. No error is thrown in most languages β the value just silently becomes wrong. This is integer overflow.
Common integer types and their limits:
Notice that uint8 maxes out at 255 β which is FF in hex and 11111111 in binary. These three representations of the same limit appear constantly once you understand them: the maximum CSS colour channel is 255 (FF), the maximum TTL field in an IP header is 255, the maximum value of a single byte in any context is always 255.
Knowing integer limits in hex helps catch overflow bugs: if a 32-bit counter reaches 0xFFFFFFFF (4,294,967,295) and you add 1, it wraps to 0x00000000. Packet sequence numbers in TCP and GPS week numbers have both caused real-world incidents from exactly this wrap-around.
For a deeper look at how text is stored as binary underneath all of this β the encoding layer between characters and bytes β see our guide to Unicode and UTF-8 character encoding.
If you want to understand how binary data gets compressed before it is stored or transmitted, our guide on how file compression works picks up where this one leaves off.
How to Convert Without Doing the Math
For one-off conversions, a dedicated online tool is faster than doing it by hand. Use FileCrank's free tools:
- Number Base Converter β convert any number between binary, octal, decimal, and hex simultaneously
- Hex Converter β hex to decimal, hex to text, and back
- Binary Converter β binary to text and decimal
- ASCII Converter β character codes and their decimal/hex equivalents
These tools handle all bases at once, so you can paste in 2A and instantly see 42 in decimal, 52 in octal, and 101010 in binary.
Quick Reference: Common Conversions
Notice that 255 in hex is FF β the maximum value of a single byte. This is why colors go from #000000 (black) to #FFFFFF (white).
Conclusion
Number base conversion is one of those foundational CS skills that pays off constantly β in debugging, in reading documentation, in understanding how data is stored and transmitted. Binary is what the machine sees. Hex is what engineers read. Decimal is what humans prefer.
Once you understand that these are just different ways of writing the same number, moving between them becomes second nature. Try FileCrank's Number Base Converter to practice with your own values.