Estimated difficulty: 💚💚💚
Hey Securiteenies! This week we’re covering a few common numerical systems in the technology world – mostly because they’re really cool, but also because you might come across these in the wild sometime and it’s useful to know how they work and understand what they mean.
We’ve tried to keep this post nice and simple, but if there’s anything you’re not sure about, ask us a question at the bottom of the page. 🥰
Denary (Base 10)
To start off, before we explain how binary, hexadecimal or base 64 work, we’ll look at a numerical system you already understand – denary (decimal – base 10).
Denary uses characters 0-9, with 9 being the largest number you can have of a particular unit. Each column to the left is 10 times larger than the one to the right and each column to the right is 10 times smaller. This is why we call it base 10 – because there are 10 values, and it’s based on multiplications of 10. You might see denary notated as 0d at the beginning of the block.
Example:
900 + 90 + 9 = 999
999 is equal to 9 hundreds, 9 tens, and 9 units (ones).
999 is the largest value we can have in three columns.
Adding 1 to this makes it 1000 - 10 times larger than the last column (or another
power of the base unit - this will be important later).
If a column is “empty” we use a 0 as a place holder, so we don’t assume the number is smaller than it is. Here are a few examples:
100000 (10^5) (Hundred thousands) | 10000 (10^4) (Ten thousands) | 1000 (10^3) (Thousands) | 100 (10^2) (Hundreds) | 10 (Tens) | 1 (Units) | |
761296 | 7 | 6 | 1 | 2 | 9 | 6 |
340530 | 3 | 4 | 0 | 5 | 3 | 0 |
100420 | 1 | 0 | 0 | 4 | 2 | 0 |
Binary (Base 2)
Binary is similar to denary, but with a base of two instead of ten – it only uses 1 and 0. Rather than each column being 10 times larger/smaller, it’s a power of two, so the column doubles in size as you move to the left or halves as you move to the right.
Sometimes if a column contains a 1 it’s referred to as being “on” and a 0 is referred to as being “off” – this might be an easier way for you to think about it. Computers use something called logic gates, which are basically electronic switches that can be on or off – a group of logic gates forms a logic circuit, and a group of logic circuits might form a microprocessor, or computer memory. The logic gates use binary (or boolean operators) to store or work with data.
Binary is written in groups of eight “bits”, which we call a “byte” altogether. If you have 1000 bytes, you have a kilobyte. 1000 kilobytes is a megabyte, 1000 megabytes is a gigabyte, and so on. (1024 bytes is a kibibyte, 1024 kibibyte is a mebibyte – these follow the binary numerical system rather than the metric system.) Binary is usually notated by 0b at the beginning of a block so you know it isn’t base 10.
Here are some binary number examples:
128 (2^7) | 64 (2^6) | 32 (2^5) | 16 (2^4) | 8 (2^3) | 4 (2^2) | 2 | 1 | |
127 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
33 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 |
96 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 |
40 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 |
99 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 |
Usually in binary, the bigger bits are on the left (like with decimal).
Sometimes when people write binary numbers they might leave off the leading bits - so if they're only writing the number 10 (in decimal), they might write:
1010
Rather than:
00001010
Watch out for this, so it doesn't confuse you.
So for the number 10 (00001010 in binary) – the columns for 2 and 8 are “switched on”, if you wanted to write the number 15, it would be 00001111 – the columns for 8 + 4 + 2 + 1 are all switched on.
Hexadecimal (Base 16)
Base 10 has 10 values and each column is ten times larger, base 2 has 2 values and each column is twice as large – so base 16? Yep – 16 values, and each column is 16 times larger. Hexadecimal (sometimes just called hex) is usually indicated by a 0x at the beginning – so you know it’s hex and not base 64.
So hexadecimal values are 0-9, then A-F, like this:
Decimal | Hexadecimal | Decimal | Hexadecimal | |
0 | 0 | 8 | 8 | |
1 | 1 | 9 | 9 | |
2 | 2 | 10 | A | |
3 | 3 | 11 | B | |
4 | 4 | 12 | C | |
5 | 5 | 13 | D | |
6 | 6 | 14 | E | |
7 | 7 | 15 | F |
Because hexadecimal has more values than binary or decimal, it’s a much more efficient numerical system – and some people think it’s a little easier for humans to read (when you understand it) than binary. It only takes 2 bits of hexadecimal to show the same number as 8 bits – a whole byte of binary. 🤯
Imagine we wanted to write the number 255. In binary this would be:
11111111
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
But in hexadecimal this would be:
FF
(16 x 15) + (1 x 15)
Keeping this reasonably small (because 16 x 4096 is a bit unnecessary for the purpose of this explanation) here are some example numbers:
4096 (16^3) | 256 (16^2) | 16 | 1 | |
32 | 0 | 0 | 2 | 0 |
4095 | 0 | F | F | F |
127 | 0 | 0 | 7 | F |
203 | 0 | 0 | C | B |
2596 | 0 | A | 2 | 4 |
55984 | D | A | B | 0 |
4919 | 1 | 3 | 3 | 7 |
Hexadecimal is used to specify colours on the web too – you string three bytes (6 digits) of hex together to tell you the red, green and blue (RGB) values of a colour. (Example: The hex codes for black and white are #000000 and #FFFFFF.) Because hexadecimal is so efficient, it only takes 6 bits to store this information rather than the 9 bits of decimal it would need (for three numbers between 0-255), and this means we can represent nearly 17 million colours with only 6 bits – which is super cool. 🤩
Now for the part where this all ties together! You’re probably thinking “Okay, maybe this works for numbers – and maybe even colours, but this is pretty useless for anything else…”
I present to you the American Standard Code for Information Interchange (ASCII – “aski”). Just like how International Morse Code converts alphanumeric characters into a more easily transmissible format, ASCII assigns functions and printable characters to numbers – to make it simpler and more efficient for computers to handle and process.* If you want to read more about the development of ASCII, this section on Wikipedia isn’t too bad.
*ASCII is based on the English alphabet, there are other encodings and standards for different alphabets, and other encodings (e.g. UTF-8 or 8-bit Unicode Transmission Format) are more common now and have a much larger range of characters.
Base 64
Okay! So then, base 64? You got this. 😉
Base 64 uses ASCII characters as its base values (like hex with 0-9 and A-F) because they’re universal and can be transferred reliably without error. Usually base 64 is used for encoding things like images or other binary data – we’ll look at an example of this below, but here’s an index table to show you the values:
Index | Binary | Char | Index | Binary | Char | Index | Binary | Char | Index | Binary | Char | |||
0 | 0000000 | A | 16 | 0010000 | Q | 32 | 0100000 | g | 48 | 0110000 | w | |||
1 | 0000001 | B | 17 | 0010001 | R | 33 | 0100001 | h | 49 | 0110001 | x | |||
2 | 0000010 | C | 18 | 0010010 | S | 34 | 0100010 | i | 50 | 0110010 | y | |||
3 | 0000011 | D | 19 | 0010011 | T | 35 | 0100011 | j | 51 | 0110011 | z | |||
4 | 0000100 | E | 20 | 0010100 | U | 36 | 0100100 | k | 52 | 0110100 | 0 | |||
5 | 0000101 | F | 21 | 0010101 | V | 37 | 0100101 | l | 53 | 0110101 | 1 | |||
6 | 0000110 | G | 22 | 0010110 | W | 38 | 0100110 | m | 54 | 0110110 | 2 | |||
7 | 0000111 | H | 23 | 0010111 | X | 39 | 0100111 | n | 55 | 0110111 | 3 | |||
8 | 0001000 | I | 24 | 0011000 | Y | 40 | 0101000 | o | 56 | 0111000 | 4 | |||
9 | 0001001 | J | 25 | 0011001 | Z | 41 | 0101001 | p | 57 | 0111001 | 5 | |||
10 | 0001010 | K | 26 | 0011010 | a | 42 | 0101010 | q | 58 | 0111010 | 6 | |||
11 | 0001011 | L | 27 | 0011011 | b | 43 | 0101011 | r | 59 | 0111011 | 7 | |||
12 | 0001100 | M | 28 | 0011100 | c | 44 | 0101100 | s | 60 | 0111100 | 8 | |||
13 | 0001101 | N | 29 | 0011101 | d | 45 | 0101101 | t | 61 | 0111101 | 9 | |||
14 | 0001110 | O | 30 | 0011110 | e | 46 | 0101110 | u | 62 | 0111110 | + | |||
15 | 0001111 | P | 31 | 0011111 | f | 47 | 0101111 | v | 63 | 0111111 | / |
So, let’s take a quick example to explain how this works – to make it easier to explain, we’ll use the word “Heart”.** ❤️
Firstly, the word is broken down into the binary for each letter:
H - 01001000
e - 01100101
a - 01100001
r - 01110010
t - 01110100
Then, these are concatenated - strung together, like this:
0100100001100101011000010111001001110100
Now, base 64 works with groups of 6 bits (2^6, like in our binary table above) - with one base 64 character presenting 6 bits of binary. We take the string above, and split it out into blocks of 6:
010010 000110 010101 100001 011100 100111 0100=*
These 6-bit strings are the following in base 64:
SGVhcnQ=
Decoding base 64 is just the reverse of this process! The base 64 values are converted into their 6-bit binary values, which are then strung together, split back out into 8-bit bytes, and then converted back into their original format. It’s important the padding is right when it’s time to decode a message.
Here’s a base 64 encoder/decoder if you want to try it out and see how it works!
*Padding is used if there aren’t quite enough bits for a whole byte, or say if you need a space holder at the end of a message so the numbers aren’t altered.
**Remember that if the original data is already in ASCII, there’s not much point in encoding it into base 64 ASCII to transfer it – that’s not to say it doesn’t happen, but you’ll probably see this most often with other kinds of data.
Exercise
Have a go at the problems below (without using a converter – no cheating) to test your understanding! Check your answers with the table above. 😊
💚🤍🤍 Easy peasy: Try writing your name in binary.
Example: Here’s my name (Morgan) in binary.
01001101 01101111 01110010 01100111 01100001 01101110
💚💚🤍 Okeydokey: Write your date of birth in hexadecimal.
Example: Here’s the date of our first post on Security Queens (10/04/2020 – DD/MM/YYYY British date format).
31 30 2F 30 34 2F 32 30 32 30
💚💚💚 Kinda hard: Using the ASCII table above, can you figure out how octal (base 8) works?
Hint: Base 10 uses numbers 0-9, base 16 uses numbers 0-15, base 2 uses 0-1…
If you want some more exercises for binary and hex, check out this GCSE Bitesize page. 💁♀️
Thank you for reading! Binary was my first love, but hex and I are getting pretty serious these days… 😉
Morgan x