What is a hexadecimal number?

We learn in our childhood to write numbers with the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. These ten digits are part of the decimal system (or base 10) most commonly used today and this notation feels so natural that we rarely think about it.

But a number is only the representation of a quantity and we can write it in many different ways. We can for instance write the quantity ‘ten’ in full letters (‘ten’), with the digits ‘1’ and ‘0’ (’10’) of the decimal system or with the letter ‘X’ of the Roman numerals.

We (usually) have ten fingers and that explains why many (not all) civilizations have chosen a ten-digit base system for representing numbers. Computers on the other hand do not have fingers, but they use transistors which process data in the form of bits with the values ‘0’ or ‘1’ (in base 2). The number ‘255’ of the decimal system is therefore written as ‘11111111’ in base 2. You can find out more about the powers of two in this page.

What are hexadecimal numbers then?

Hexadecimal numbers (or the hexadecimal numeral system) use 16 symbols (base 16) instead of 10 or 2 to represent numbers. They use the symbols ‘0’ to ‘9’ for the values 0 to 9 in the same way as in the decimal numeral system and the letters ‘A’ to ‘F’ to represent the values from 10 to 15. The following table contains the representation of the first sixteen numbers using the decimal, binary and hexadecimal numeral systems.

Conversion table:

Decimal (base 10) Binary (base 2) Hexadecimal (base 16)
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

Hexadecimal numbers are used frequently in computer science because 16 is a power of 2 (16 = 24) and this representation requires fewer digits than with the ‘0’ and ‘1’ of the binary numeral system. For instance, the number 2021 (in the base 10 decimal numeral system) is written ‘11111100101’ in binary and ‘7E5’ in hexadecimal.

The conversion between the base 2 and the base 16 is simple because 4 bits (in base 2) can be used to represent 16 different values. We can therefore split a set of bits into subsets of 4 bits starting from the right. The corresponding hexadecimal symbol is then associated with each 4-bit subset in order to obtain the hexadecimal representation.

Example: Writing the binary number ‘11111100101’ in hexadecimal:

Binaire à Hexadécimal

Use and representation of hexadecimal numbers

Hexadecimal numbers are often used to represent binary data, to specify the RGB colors components of elements in a web page or to identify error codes for example. Since they share the digits ‘0’ to ‘9’ with decimal numbers, a prefix ‘0x’ or ‘#’ is used to identify when a number is using the hexadecimal numeral system: ‘0xFFAA01’ or ‘#FFAA01’ for example. The letter case is not important in hexadecimal numbers, therefore ‘0xFFAA01’ and ‘0xffaa01’ represent the same number for instance.

You can find a table with the first 256 hexadecimals numbers and their equivalents in binary or decimal on this page as well as a binary/decimal/hexadecimal converter on this page.

The post What is a hexadecimal number? appeared first on EatYourBytes