Hex and ASCII
Let’s learn about the hexadecimal number system and American Standard Code for Information Interchange (ASCII).
We'll cover the following
Hex
The main reason we need hex is that it provides a compact way of representing binary numbers. This is particularly helpful for cryptographic keys, which are often very long when represented in binary.
Writing a number in hex
We use the base number 16 instead of 10 in decimal (and 2 in binary) for hex numbers. Thus:
-
The digits of a hex number can, in theory, take any of the values between 0 and 15.
-
Every digit in a hex number will be multiplied by some power of 16. The powers of 16 start with 0 (the furthest digit to the right) and then increase from right to left.
The first requirement causes a slight problem since it will be hard to determine whether 12 means the digit 1 followed by the digit 2, or whether we mean the hex ‘digit’ 12. Thus, we replace the hex digits 10, 11, 12, 13, 14, and 15 with A, B, C, D, E, and F, respectively. So, in practice, the digits of a hex number take any of the values between 0 and 9 and between A and F.
Converting hex to decimal
Converting hex to decimal follows the same principles as binary to decimal, except now we are using multiples and powers of 16. Consider the hex number . It follows that:
Thus, B5F in hex is 2911 in decimal.
Here, we have a coded example of the hex to decimal conversion defined above:
Get hands-on with 1200+ tech skills courses.