Convert between hex, decimal, octal and binary

The most widely used numeral system is no doubt the decimal system (base 10) which uses the digits 0-9 but in many computer related situations it can sometimes be useful to work with other numeral systems, the most common being the binary, octal and hexadecimal numeral systems.


The binary numeral system (base 2) uses only two digits, 0 and 1. A binary digit is often called a bit and is the smallest unit of information that can be stored on a computer. Multiple bits are often grouped together to form a byte, and sometimes even bigger units, which can be used to represent larger numbers.

Computer programmers often use the decimal numeral system, just like the rest of the population, but binary can sometimes be useful in situations where the value of the individual bits are more important than the number that they represent together. Many programming languages uses the prefix 0b to write binary numbers. For example, 13 can then be written as 0b1101.

Writing small numbers in binary is often not a problem but larger numbers can be hard to read because binary requires many more digits compared to the same number written in decimal. For this reason it is much more common to use hexadecimal or octal numbers instead.


The octal numeral system (base 8) uses the eight digits 0-7. One of the main reasons why octal is sometimes used is because each octal digit correspond exactly to three binary digits. This is because the number of values that can be represented using 3 bits ( 2^3 ) are equally many as the number of octal digits ( 8 ). This means that if you change one bit it will only affect one of the octal digits.

Binary Octal Decimal
111101100 754 492
111111100 774 508

In the above example you can see how the digits marked in blue change for each numeral system when one of the bits changes. Note how the decimal number changes completely. It is relatively easy to learn how to convert between octal and binary in your head, but with decimal it becomes much more difficult.

Many programming languages use the prefix 0 (zero) to write octal numbers. This can sometimes be problematic because the normal mathematical convention is to ignore leading zeroes. It is therefore not uncommon for people who are new to programming, and doesn't know about the 0 prefix, to try and write decimal numbers with leading zeroes. If they are lucky the number will contain the digits 8 or 9 which will give them an error message because of invalid octal digits, but in other cases it can lead to bugs that make the program behave incorrectly.


The hexadecimal numeral system (base 16) uses 0-9 for the ten first digits, and A-F for the rest. In programming the prefix 0x are often used when writing hexadecimal numbers.

Hexadecimal numbers have the same type of advantages as octal numbers but each digit correspond to 4 bits instead of 3. Since a byte consist of 8 bits it is possible to use two hexadecimal digits to represent the value of a byte. This can often be a big advantage because it makes it much easier to see what the values of the individual bytes are, and it explains why hexadecimal numbers are much more widely used in practice compared to octal numbers.