Base Systems and ASCII

Speaking the same language as your computer

Ciaran Mcveigh
blockchaintechnologies

--

Have you ever heard that computer code is ultimately just 1’s and 0’s?

Well it is and in this article I will explained to you how those 1’s and 0’s are interpreted by your computer to become the letters and symbols you see on your keyboard. To do this we’re going to take a look at the various base numeral systems and how these numeral systems are used by your computer.

The Base Systems

We will cover 3 of the base systems,

Binary: Base 2
Decimal: Base 10
Hex: Base 16

Before we begin let me give you an example, all the numbers below have the same numeric value,

Binary:10110000
Decimal: 176
Hex: B0

Let’s start with decimal since this is the base we all know and love. Decimal is base 10 meaning you have 10 potential characters to choose from (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and that these characters will be multiplied by some power of 10.

As an example 176 actually means (1 x 10² + 7 x 10¹ + 6 x 10⁰) hence the base 10.

The hex system is base 16 and works in the same manner as decimal. It uses the same characters as decimal with the additional (A, B, C, D, E, F) where A = 10, B = 11 etc. Note both lower and upper case characters are valid in hex and correspond to the same numeric value.

If we look at B0 we can compute its value using the same method we used with decimal except with a base of 16 this time. B0 becomes(11 x 16¹ + 0 x 16⁰) which is the same as the decimal value 176. Hex enables us to represent larger numbers in a smaller number of characters.

Be careful hex can still look like a decimal number, for example 41 in hex is actually 65 in decimal (4 x 16¹ + 1 x 16⁰). Because of this programming languages will often prefix it with a 0x to let you know this value is a hexadecimal so 41 would be presented as 0x41.

Finally we have binary which is base 2, with 2 available options 1 and 0. The 1 and 0 represent on and off in an electrical component within a computer. Again we use the same calculation method, this time with a base of 2. 10110000 becomes (1 x 2⁷ + 0 x 2⁶ + 1 x 2⁵ + 1 x 2⁴ + 0 x 2³ + 0 x 2² + 0 x 2¹ + 0 x 2⁰).

The table below represents 8 bits also known as a byte and can take any value between 0–255. Have a go for you self-pick a number and see what combination of bits you need to make it. We can see that each bit represents a number and that these numbers double working from right to left. Note this doubling process can go on infinite number of times.

               +-----+----+----+----+---+---+---+---+
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
+-----+----+----+----+---+---+---+---+
| 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
+-----+----+----+----+---+---+---+---+

ASCII

So how do these numbers become readable text? Well that is where the ASCII table comes in. When binary is converted to ASCII each character is represented by 8 bits or one byte.

Note that each hex character represents 4 bits also know as nibble. This makes sense since both can have a numeric value between 0 and 15

As an example lets look at the word “dog”. This contains three characters d, o, g which are represented by decimal numbers 100, 111, 103. These decimal numbers are predefined and can be found in the ASCII table referenced above. Below is a comparison of the hex and binary representations.

ASCII: d / o / g
Hex: 64 / 6F / 67
Binary: 01100100 / 01101111 / 01100111

Below is a python script which performs many of the transformations discussed in this article. From binary to hex, hex to decimal and decimal to ascii. Experiment with the code, passing in different values to the functions and inspecting what each function does under the covers to gain a deeper understanding of the concepts described in this article.

CHALLENGESee if you can implement the following functions into the above code def binaryToBytes(binary):
return 'TODO'
def binaryToHex(binary):
return 'TODO'
Solution: Coming Soon

Hope this article helped, any corrections or improvements are always welcome.

--

--