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.
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:
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:
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:
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:
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.
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:
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.
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… 😉