In this video from ITFreeTraining I will look at how to convert decimals- a skill that is
required for working with IPv4 and IPv6.
This video will look at binary, hexadecimal, and how they relate with bytes and other units.
I will look at how you would convert between these. When you start working with networking,
this becomes an important skill to have, particularly if you start designing networks. So, let’s
get started.
First of all, what is binary? Binary is a system that uses values or switches that are
either on or off. If the switch is on, the value is 1. If the switch is off, the value
is 0. This gives you two possible values. However, if you group multiple binary values
together it is possible to represent larger numbers. A common way of doing this grouping
is to group the binary digits into a string. Shown here is an example of a binary string.
In this example, 8 binary values have been combined together in a string. An individual
zero or one is known as a bit. So in this example there are 8 bits in the binary string.
The next step to understanding binary is how would a binary string get converted to a decimal?
To understand this, consider the power of 2 table. 2 to the power of 0 gives a value
of 1. If you were to count from zero, that is zero then one, this would give you two
values which allows you to represent a binary switch, which is 1 or 0.
If you consider the next row, 2 to the power 1 gives a value of 2. So the value has doubled.
The next row 2 to the power of 2 gives a value of 4. The power of 2 table becomes important
when converting from binary to decimal or decimal to binary. For this reason it is a
good idea to memorize this table. Let’s now see how this table is used to convert
a binary number to a decimal.
In this example, I want to convert the binary string into a decimal. To do this, I will
build a conversion table. Using this conversion table, I will convert my example binary string
to a decimal number.
First of all I will add the power of 2 values. To start with, I will add 2 to the power of
0 which gives a value of 1. Next I will add 2 to power 1 giving a result of 2. I think
you get the idea, so I will add the rest of the values.
Now that I have a conversion table with the powers of 2 in it, I will next add my example.
All the information is now there to make the conversion from binary to decimal. The next
step is to work out the value that each column or bit has. This is a simple process.
If I start with column 8, the example has a 1 in this column, so the value for this
column will be 128. If the binary is 1, or on, convert the value from the powers of 2
into the value row. If I now consider the next column, this has a binary value of 0,
or off. So in this case the value is 0. That is all there is to it. If the binary value
is zero, the value will be zero. If the binary value is 1, copy the value from the power
of 2 into the value. Another simple way to do it is to multiply the values together.
1 times 128 is 128. 0 times 64 is 0.
Regardless of how you do it, you can see that it is an easy matter to complete the results
of the table, which I will do now. Column 6 binary value is 0 so the value will be 0,
column 5 binary value is 1 so the value will be 16. Column 4 binary value is 1 so the value
will be 8. Column 3 binary value is 0 so the value will be 0. Column 2 binary value is
1 so the value will be 2. Lastly, column 1 binary value is 0 so the value will be 0.
Now that all the values have been worked out, the next step is to add them together to get
the result. Looking at the table, column 8 is 128, column 7 and 6 are zero, column 5
is 16, column 4 is 8, column 3 is zero, column 2 is 2 and column 1 is 0. Adding all these
values together gives a final value of 154. So the example binary string represent at
the decimal number 154.
If you are given a binary string, it is a simple matter to follow this procedure to
convert a binary string to a decimal. The next step is to convert a decimal to binary.
The procedure is much the same, so let’s have a look.
In this case, I will convert my previous example of 154 to binary. Once again, I will create
my conversion table. Just like before, I will add the Powers of 2 to the table. So far nothing
has changed from the previous example.
The next step is to perform a number of tests on how to work out what the bits are. To do
this, I will take the example of 154 and compare it to the value in the powers of 2. In this
case it is 128. So I want to know is 154 greater than or equal to 128. The answer is yes, so
I will record a 1 in the results. If the value was less than 128, I would instead have recorded
a zero.
Next I will go to column 7. Since the previous column resulted in a 1, I need to subtract
that value. So 128 subtracted from 154 gives a value of 26. Like before, I need to compare
this value to the powers of 2. 26 is not greater than or equal to 64 so I will record a zero in the results.
The process repeats again. For the next value, 26 is not greater than or equal tro the powers of 2 value
of 32 and thus a zero will be recorded.
Since no 1’s are being record, the same comparison value of 26 gets used each time
and does not change. Next, 26 is compared to 16, 26 is greater or equal to 16 and thus
a 1 is recorded.
Now that a 1 was recorded, the next comparison value needs to change. It will be 26 minus
16 giving a value of 10. 10 is greater or equal to the power of 2 value 8 and thus a
1 will be recorded. Since 1 was recorded, the next comparison value will have 8 subtracted
from it giving a value of 2. 2 is not greater or equal to 4 so a 0 will be recorded.
For the next comparison, 2 is compared to 2 and is equal, and thus a 1 is recorded.
For the last column, since a 1 was recorded in the previous column, 2 will be subtracted
from the comparison value. Zero is not greater than or equal to 1 so 0 is recorded.
Now that all the bits have been worked out, they can be put together to form a binary
string. You can see that converting a decimal number to binary is a simple task. It just
involves some comparison and subtraction to get the result. Now that you know how to do
some conversions, I will now look at the types of data you may come across.
The most common data type you will most likely come across is the byte, but it does help
to know the other ones. The smallest data type is a single bit. Once you start grouping
bits together, these form other types. The next one for example is 4 bits referred to
as a Nibble. A Nibble is useful in the next section when I look at hexadecimal.
The next data type is the byte. Even though there are smaller types like nibbles and bits,
a byte is often the smallest type that is used. For example, when writing to a file
on a hard disk you will only be able to write data as multiples of bytes. For example, you
will not be able to write a single bit.
The next data type is a word. This is 16 bits or two bytes. Sometimes you will use this
in networking but not too often.
The next type is long word. This is 4 bytes or 2 words. Since it is 2 words it is sometimes
referred to as a double word. With IPv4 you come across this data type since IPv4 addresses
are 32bits long.
The last data type is 64 bits known as a very long word. This is also referred to as a Quad
Word. Data types of this size you will come across if you use IPv6, as a 128 bit IPv6
address is often broken into two 64bit parts. Now that I have had a look at all the data
types, I will next look at hexadecimal.
Hexadecimal uses 16 as a base. Since the standard number system that we use is base 10, this
means that there are 6 values that we cannot use a single digital to represent. To make
up for this, letters A through F are used. As shown in the table here, 0 to 9 are the
same for both decimal and hexadecimal. The values 10 through 15 change to letters. For
example 10 is the letter A and 15 is the letter F.
I will now have a look at how to convert the example of 154 to hexadecimal. 154 has already
been converted to binary. The next step is to divide the 8 bits shown here into two 4
bit parts. Just like when converting numbers to and from binary the power of 2 will be
used. Since there are only 4 bits, only 8, 4, 2 and 1 will need to be used.
To perform the last step it is just a matter of converting the 4 bit value to a decimal.
In the case, the left bits, when added together, give the result of 9. Looking up this value
in the table gives the same value of 9. So the left part is 9 in hexadecimal.
If I now look at the right 4 bits, the binary gives the decimal value of 10. If I look this
up in the table, this gives me a hexadecimal value of A. To get the final result I need
to combine the left and right sides together. This gives me the final result of 9A.
You can see that the conversion process is not too difficult when you know how. If you
want to convert the other way, it is a simple matter to reverse the process.
I looked at how to do the process manually, but now I will have a look at how to achieve
the same results using the Windows Calculator. When the Windows Calculator is opened, it
may open in a view that is not that useful to us. In this case it has opened in the “Standard”
view. To change the view, I will select the view menu and then select the view “Programmer”.
The programmer view allows a quick and simple way to convert between different types of
units. To demonstrate this, I will enter in the previous example of 154. Notice that when
I enter in the value the binary for that value is shown below.
Also notice that up to 64bits can be shown here. Having so many bits on the screen can
make it harder to read. To reduce the number of bits shown, at the bottom left of the screen
are some options. By default QWord is selected, which is short for quad word.
To reduce the number of bits that are shown I will select the DWord option which is short
for double word. Notice that once selected, the number of bits shown on the screen has
been reduced to 32.
If I select the Word option, this will reduce, as you would expect, the number of bits down
to 16. The last option, “Byte”, will reduce the view down to 8 bits, however notice that
when it was selected the value changed from 154 to -102. This is because Windows calculator
uses signed values. Signed values essentially means that one of the bits affects if the
value will be positive or negative. When working with networking you want to use unsigned values.
Unfortunately this option cannot be changed in Windows Calculator.
In this case I am working with a single byte so I will select the word option and then
enter the value in again. Selecting a large value like word when working with a byte means
I won’t have to worry about signed values. With the value entered, if I want to change
it to hexadecimal, it is a simple matter to select the option Hex on the left side.
When one of these options is selected on the left hand side, whatever value is currently
in the calculator will be converted. In this case the value of 154 is converted to the
hexadecimal value of 9A.
Even though the binary value is shown, I can change the value shown in the calculator to
binary by pressing the “Bin” option. In some cases you may want to convert a binary
value to a different type. In this case, I will clear the value in the calculator and
enter in a new value. Once the new value has been entered in, if I press the hex button
this will convert the binary value to hexadecimal. Likewise, I can press the Dec option to convert
the option to decimal.
This covers the different data types used in networking and how to convert between them.
I hope you have enjoyed this video and I hope to see you in other videos from us. Until
next time, thanks for watching.