Wednesday, 2 February 2011

converting between bases

Introduction to decimal, binary, hexadecimal, octal and converting between bases

Decimal

We count in what is called the decimal counting system. Decimal has 10 digits which are 0,1,2,3,4,5,6,7,8,9. Decimal is also called base 10 because it has 10 digits. The reason why people started counting in decimal is because it has 10 digits and we have 10 fingers and people used to use their fingers for counting.

Binary

Binary uses only 2 digits which are 0 and 1 and is also called base 2. Binary is what computers use for counting because inside a computer you get things that are like little switches that can be either off or on(0 or 1). A subscripted 2 is placed after a binary number so that it can be recognised as being a binary number like in the following example:
10012
We already know that when we count we add 1 to the current number each time to get the next number. If we start at 0 and then add 1 we get 1. Now we have to add 1 to 1 but we have run out of digits because there are only 2 digits in binary. When we get to 9 in decimal and add 1 we first change the number to a 0 and then add 1 to the imaginary 0 to the left of it which gives us 10. If we apply this to binary then we change the 1 to 0 and then increase the imaginary 0 to the left of it which gives us 10 but this 10 is actually 2. The following sequence shows you how to count to 5 in binary:
0 1 10 11 100 101

Hexadecimal

The word hexadecimal is made up of 2 parts which are hex(6) and decimal(10). If you add 6 and 10 together you get 16 and that is how many digits there are in hexadecimal. Hexadecimal is sometimes called hex or base 16. To get 16 digits we have to use letters of the alphabet and those 16 digits are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. Hexadecimal is often used instead of binary numbers because just 2 hexadecimal digits can make the same numbers as 8 binary digits which in turn make up a byte. A subscripted 16 is used to show that a number is hexadecimal:
7F16

Octal

Octal uses 8 digits which are 0,1,2,3,4,5,6,7 and is also called base 8. We use a subscripted 8 to show that a number is octal:
2758

Converting from decimal to binary, hexadecimal and octal

decimal -> binary2
5 -> binary2
First write a few 2s(because it's base 2) to the power of the numbers 0,1,2,3,...
23 22 21 20
Next calculate the values of these numbers.
8 4 2 1
The highest number must be greater than the number you want to convert.
Now divide the number to be converted by the number starting on the left. Write how many times the number divides into it underneath the number and then write your remainder on top of the next number to be divided. You then divide by the remainder each time after that.
5 5 1 1
8 4 2 1
0 1 0 1
The bottom line of numbers is the binary number. Make sure to leave out all leading 0s
5 -> 1012
decimal -> hexadecimal16
Decimal to hexadecimal works in the same way except that you use 16 to the power of those numbers since that is the base this time.
26 -> hexadecimal16
162 161 160
26 26 10
256 16 1
0 1 10
Before we write the answer we must change all numbers greater than 9 to their hexadecimal equivalents.
1 10
1 A
26 -> 1A16
decimal -> octal8
This time we use 8 to the power of those numbers because we are working with base 8.
12 -> octal8
82 81 80
12 12 4
64 8 1
0 1 4
12 -> 148

Converting from binary, hexadecimal and octal to decimal

binary2 -> decimal
11012 -> decimal
First write 2 to the power of the numbers 0,1,2,3,...
23 22 21 20
Now change them to their real values.
8 4 2 1
Now put the binary number underneath the other numbers and multiply the top number by the number beneath it and put the answer underneath the other 2 with + between each number and add the bottom row together to get your final answer.
8 4 2 1
1 1 0 1
8+4+0+1 = 13
11012 -> 13
hexadecimal16 -> decimal
Once again you use 16 instead of 2.
1F16 -> decimal
161 160
16 1
1 F
16+F = 16 + 15 = 31
1F16 -> 31
octal8 -> decimal
258 -> decimal
81 80
8 1
2 5
16+5 = 21
158 -> 21

Converting from hexadecimal to binary

hexadecimal16 -> binary2
2C16 -> binary2
First write the number as decimals.
2 12
Now divide each number by the numbers 8 4 2 1.
2    12
8421 8421
0010 1100
Take the leading 0s away and you have your answer.
2C16 -> 1011002
binary2 -> hexadecimal16
101112 -> hexadecimal16
First separate the binary number into groups of 4 digits. If the number at the front has less than 4 digits then add 0s to the front of it.
0001 0111
Now use the numbers 8421 and multiply each time and add the results together for each group to get the answer.
0001 0111
8421 8421
0+0+0+1 = 1; 0+4+2+1 = 7
101112 -> 1716

Converting from octal to binary

Octal to binary uses the same method as hexadecimal to binary except that you use the numbers 421 and groups of 3 digits.
octal8 -> binary2
268 -> binary2
2   6
421 421
010 110
268 -> 101102
binary2 -> octal8
10102 -> octal8
001 010
421 421
0+0+1 = 1; 0+2+0 = 2
10102 -> 128

Converting from hexadecimal to octal

We do not convert directly from hexadecimal to octal but instead first convert to binary and then to octal.
hexadecimal16 -> octal8
4516 -> octal8
First convert the hexadecimal number to binary.
4     5
8421 8421
0100 0101
Now make groups of 3 digits and then convert to octal.
001 000 101
421 421 421
0+0+1 = 1; 0+0+0 = 0; 4+0+1 = 5
4516 -> 1058
octal8 -> hexadecimal16
278 -> hexadecimal16
First convert the octal number to binary.
2   7
421 421
010 111
Make groups of 4 and then convert to hexadecimal.
0001 0111
8421 8421
0+0+0+1 = 1; 0+4+2+1 = 7
278 -> 1716

How to remember

All of these conversions follow certain patterns that you need to remember. When converting from decimal you always use divide and when converting to decimal you always multiply. Converting between hexadecimal and binary as well as octal and binary is a bit easier to remember. Just remember that hexadecimal is 8421 and octal is 421. The only thing you need to know about converting between hexadecimal and octal is that you must always convert to binary first.

No comments:

Post a Comment