Binary, Octal and Hexadecimal Explained, With Worked Examples
Every number system works the same way as the one you learned at school; only the size of the steps changes. Here is how to read binary, octal and hex, convert between them by hand, and spot them in real work.
By the Fileora team7 min read
A homework question asks for 255 in hexadecimal. A colour in a stylesheet is written #FF8800. A server guide tells you to run chmod 755. All three are the same idea wearing different clothes: a number written in a base other than ten.
The good news is that there is nothing new to learn. If you can read 347 as "three hundreds, four tens and seven ones", you already understand every number system in this guide.
One rule for every base
Decimal numbers grow by a factor of ten with every step to the left: ones, tens, hundreds, thousands. Ten is the base, and there are ten digits, 0 to 9.
Change the base and only two things change: how many digits exist, and how much each place is worth.
- Binary (base 2): digits 0 and 1. Places are worth 1, 2, 4, 8, 16, 32, 64, 128…
- Octal (base 8): digits 0 to 7. Places are worth 1, 8, 64, 512…
- Decimal (base 10): digits 0 to 9. Places are worth 1, 10, 100, 1000…
- Hexadecimal (base 16): digits 0 to 9, then A to F for ten to fifteen. Places are worth 1, 16, 256, 4096…
To read any number, multiply each digit by the value of its place and add the results. That single rule answers most exam questions on the topic.
Converting to decimal: multiply and add
Binary 1011 to decimal. The places, right to left, are 1, 2, 4 and 8.
- 1 × 8 = 8
- 0 × 4 = 0
- 1 × 2 = 2
- 1 × 1 = 1
Total: 11.
Octal 157 to decimal. The places are 1, 8 and 64.
- 1 × 64 = 64
- 5 × 8 = 40
- 7 × 1 = 7
Total: 111. This is also why octal 10 is not ten: it is one eight and no ones, so it equals 8.
Hexadecimal 2F to decimal. The places are 1 and 16, and F stands for fifteen.
- 2 × 16 = 32
- 15 × 1 = 15
Total: 47.
Converting from decimal: divide and keep the remainders
Going the other way, divide by the base again and again, writing down each remainder. When the result reaches zero, read the remainders from the last one to the first.
Decimal 13 to binary:
- 13 ÷ 2 = 6, remainder 1
- 6 ÷ 2 = 3, remainder 0
- 3 ÷ 2 = 1, remainder 1
- 1 ÷ 2 = 0, remainder 1
Read upwards: 1101.
Decimal 64 to octal:
- 64 ÷ 8 = 8, remainder 0
- 8 ÷ 8 = 1, remainder 0
- 1 ÷ 8 = 0, remainder 1
Read upwards: 100.
Decimal 255 to hexadecimal:
- 255 ÷ 16 = 15, remainder 15 (F)
- 15 ÷ 16 = 0, remainder 15 (F)
Read upwards: FF. The same number is 377 in octal and 11111111 in binary: eight ones in a row.
The shortcut between binary, octal and hex
Octal and hexadecimal exist because they are shorthand for binary. Eight is 2 × 2 × 2 and sixteen is 2 × 2 × 2 × 2, so:
- one octal digit = exactly three binary digits
- one hex digit = exactly four binary digits
That means no arithmetic is needed. Split the binary number into groups, starting from the right, and translate each group on its own.
Take 11010110.
- In groups of four: 1101 0110. 1101 is 13, which is D, and 0110 is 6. So the hex value is D6.
- In groups of three: 11 010 110, which is 3, 2 and 6. So the octal value is 326.
Going back works the same way in reverse: write each hex digit as four bits, or each octal digit as three.
When you would sooner not do it by hand, the binary converter on Fileora shows a number in all four bases at once as you type, and can group the bits in fours or eights so long values are easier to check against your own working.
Why 255 keeps turning up
Computers store data in bytes, and a byte is eight bits. Eight binary digits can show 256 different values, from 00000000 to 11111111, which is 0 to 255 in decimal.
In hex, that same range is 00 to FF: exactly two digits per byte. This is the real reason programmers like hexadecimal. A long run of ones and zeros becomes a short string where every pair of characters is one byte.
Where you meet each one
Hexadecimal: colours on the web
A web colour such as #FF8800 is three bytes written in hex: one each for red, green and blue. MDN describes each part as "a case-insensitive hexadecimal number between 0 and ff (255)" (MDN).
Read it in pairs:
- FF = 255 red (the maximum)
- 88 = 136 green (8 × 16 + 8)
- 00 = 0 blue
Full red, a good amount of green and no blue: orange. A three-digit colour is the same idea with each digit doubled, so #F09 means #FF0099. To see a colour in other notations without doing the sums, paste it into the colour converter.
Octal: file permissions
On Linux, macOS and other Unix-like systems, chmod 755 sets who may read, write and run a file. The POSIX standard says the mode may be "a non-negative octal integer", with one bit for each permission (The Open Group).
Each of the three digits covers one group (owner, group, everyone else) and is the sum of:
- 4 for read
- 2 for write
- 1 for execute
So 7 is 4 + 2 + 1, meaning all three, and 5 is 4 + 1, meaning read and execute but not write. 755 therefore lets the owner do everything, and lets others read and run the file. Octal fits perfectly here because each digit is exactly three bits: one per permission.
Binary: the layer underneath
You rarely type binary, but it is what everything else stands for. It appears directly when single bits matter: network masks, hardware flags, and the "two's complement" method computers use for negative numbers.
Base64: a cousin for carrying data
Base64 is not a number system you count in, but it grows from the same root. Hex spends one character on four bits; Base64 squeezes six bits into each character by using a larger alphabet. The standard defines "a 65-character subset of US-ASCII", in which 24-bit groups of input become "output strings of 4 encoded characters" (RFC 4648). The 65th character, =, pads the end.
In practice, every three bytes become four letters. The two-letter word "Hi" is two bytes, so it encodes to SGk=, with one = filling the gap. You will see Base64 in email attachments, in data URLs and inside web tokens. To read or produce it, try the Base64 tool.
One warning: Base64 is an encoding, not encryption. Anyone can turn it back into the original.
How to tell which base a number is in
A string like 101 could be five, sixty-five, one hundred and one or two hundred and fifty-seven, depending on the base. Programming languages remove the doubt with prefixes:
0b101is binary0o17is octal0x1Fis hexadecimal
In maths textbooks the base is written as a small number after the value instead, as in 101₂ or 1F₁₆. If neither is present, check the digits: anything containing 8 or 9 cannot be octal, and anything containing A to F must be hex.
Common mistakes
- Reading the remainders downwards. When dividing, the first remainder is the last digit. Read from the bottom up.
- Grouping bits from the left. Always start at the right-hand end and pad the leftmost group with zeros if it comes up short.
- Treating A to F as letters. In hex they are digits worth ten to fifteen.
1Ais twenty-six, not "one-A". - Forgetting that 10 changes meaning. It is two in binary, eight in octal, ten in decimal and sixteen in hex. "10" always means "one of the base".
- Dropping leading zeros in a byte. The value 5 as a byte is 05 in hex and 00000101 in binary. In a colour or a memory dump, those zeros matter.
A quick reference
- 0 to 15 in hex: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- Decimal 8 = binary 1000 = octal 10 = hex 8
- Decimal 10 = binary 1010 = octal 12 = hex A
- Decimal 16 = binary 10000 = octal 20 = hex 10
- Decimal 100 = binary 1100100 = octal 144 = hex 64
- Decimal 255 = binary 11111111 = octal 377 = hex FF
- Decimal 256 = binary 100000000 = octal 400 = hex 100
Once the place values click, the rest is practice. Work two or three conversions by hand, check them against a converter, and the pattern stays with you: every base counts the same way, just in steps of a different size.