Hex, binary and decimal: how number bases work

We count in tens because we have ten fingers, but computers count in twos, and programmers often write numbers in sixteens. These are number bases, and once you see the pattern behind them, converting between binary, decimal and hexadecimal becomes a method rather than a mystery. This guide explains place value in any base, shows how to convert by hand in both directions, uses worked examples such as a colour code and a file permission, and points out the mistakes that trip people up when they first meet 0x and 0b prefixes.

What a base is

A number base, or radix, is the number of different digits a system uses before it needs to carry into the next column. Decimal is base 10, with the digits 0 to 9. When you reach 9 and add one, you write 10: a zero in the units column and a one in the tens column.

Binary is base 2, with only the digits 0 and 1. Octal is base 8, with the digits 0 to 7. Hexadecimal, usually called hex, is base 16, so it needs sixteen symbols. It uses 0 to 9 and then the letters A to F for the values ten to fifteen: A is 10, B is 11, C is 12, D is 13, E is 14 and F is 15.

The rule is the same in every base. Each column is worth the base raised to a power, and the rightmost column is worth one. In decimal the columns are 1, 10, 100, 1000. In binary they are 1, 2, 4, 8, 16, 32, 64, 128. In hex they are 1, 16, 256, 4096.

Binary to decimal

To read a binary number, write the column values under each digit and add up the columns that hold a 1. Take 101101. From the right, the columns are 1, 2, 4, 8, 16, 32. The digits are 1, 0, 1, 1, 0, 1, so the columns with a 1 are 1, 4, 8 and 32. Add them: 1 plus 4 plus 8 plus 32 is 45.

Another example is 11111111, which is eight ones. The columns add up to 1 plus 2 plus 4 plus 8 plus 16 plus 32 plus 64 plus 128, which is 255. That is the largest value you can store in one byte, a group of eight binary digits.

A handy habit is to memorise the powers of two up to 256: 1, 2, 4, 8, 16, 32, 64, 128, 256. With those in your head, most small conversions take seconds.

Decimal to binary

The standard method is repeated division by two, keeping the remainders. Take 45. 45 divided by 2 is 22 remainder 1. 22 divided by 2 is 11 remainder 0. 11 divided by 2 is 5 remainder 1. 5 divided by 2 is 2 remainder 1. 2 divided by 2 is 1 remainder 0. 1 divided by 2 is 0 remainder 1.

Now read the remainders from the last to the first: 1, 0, 1, 1, 0, 1. So 45 is 101101 in binary, which matches the earlier example.

There is also a subtraction method. Start with the largest power of two that fits, write a 1, subtract it, and repeat. For 200, the largest power is 128, leaving 72. 64 fits, leaving 8. 8 fits, leaving 0. So 200 is 128 plus 64 plus 8, which is 11001000 in binary, with ones in the 128, 64 and 8 columns.

Hexadecimal and why programmers like it

Hex is popular because it is a compact way of writing binary. Four binary digits make exactly one hex digit, because four bits can hold the values 0 to 15. So a byte of eight bits is always two hex digits. The binary number 1100 1000 is C8 in hex: 1100 is 12, which is C, and 1000 is 8.

To convert hex to decimal, multiply each digit by its column value and add. The hex number 1A3 has columns 256, 16 and 1. The digits are 1, A (10) and 3, so the value is 1 times 256 plus 10 times 16 plus 3, which is 256 plus 160 plus 3, so 419. The number FF is 15 times 16 plus 15, which is 255.

To convert decimal to hex, divide by 16 and keep the remainders. 419 divided by 16 is 26 remainder 3. 26 divided by 16 is 1 remainder 10, which is A. 1 divided by 16 is 0 remainder 1. Reading backwards gives 1A3 again.

Worked example: a web colour

Web colours are written as a hash and six hex digits, such as #FF4A1F. The six digits are three pairs, for red, green and blue. Each pair is a byte, so it ranges from 00 to FF, which is 0 to 255.

Convert each pair. FF is 255. 4A is 4 times 16 plus 10, which is 74. 1F is 1 times 16 plus 15, which is 31. So #FF4A1F is red 255, green 74 and blue 31, which you could write as rgb(255, 74, 31): a strong orange-red.

The same logic explains why #000000 is black, because all three channels are zero, and #FFFFFF is white, because all three are at the maximum of 255. A grey has equal values in all three channels, such as #808080, which is 128 each.

Octal and file permissions

Octal is less common today, but it still appears in file permissions on Linux and similar systems. Each octal digit is exactly three bits, which suits the three permissions of read, write and execute. Read is worth 4, write is worth 2 and execute is worth 1.

A permission of 7 means 4 plus 2 plus 1, so read, write and execute. A permission of 5 means 4 plus 1, so read and execute. A permission of 6 is read and write. So 755 means the owner has 7 (all three), and the group and others each have 5 (read and execute). A permission of 644 gives the owner read and write, and the group and others read only.

Converting octal to binary is simply writing each digit as three bits: 7 is 111 and 5 is 101, so 755 is 111 101 101.

Prefixes, leading zeros and common slips

Because the digits 10 mean different things in different bases, programmers mark the base. In many languages, 0x means hex, 0b means binary and a leading 0 or 0o means octal. So 0x10 is sixteen, 0b10 is two, and plain 10 is ten. Always check the prefix before you read a number.

Letters in hex can be written in upper or lower case. FF and ff are the same. A leading zero adds no value, but it is often kept so that every byte shows two digits, as in 0A.

The most common slips are reading the columns from the wrong end, forgetting that the rightmost digit is the smallest, and mixing up the order of remainders in division. A good check is to convert your answer back to the original base. Also remember that a number with n binary digits can hold up to 2 to the power n minus 1, so eight bits go from 0 to 255, and sixteen bits from 0 to 65,535.

Tools mentioned in this guide

Frequently asked questions

Why do computers use binary?

Electronic circuits are simplest and most dependable when they only need to tell two states apart, such as on and off. Binary digits map directly onto that, and any number or character can be built from them.

What does F mean in hexadecimal?

F is the digit for fifteen, the highest value in a single hex column. FF is therefore 15 times 16 plus 15, which is 255.

How many values fit in one byte?

A byte is eight bits, which gives 256 different values, from 0 to 255. In hex that is 00 to FF.

What does 0x mean?

It is a prefix that marks the number as hexadecimal. 0x1F is 31 in decimal. Without the prefix, 1F would be unclear.

How do I convert hex to binary quickly?

Replace each hex digit with four binary digits. A is 1010 and 3 is 0011, so A3 is 1010 0011. Reverse the process by grouping binary digits in fours from the right.