2 It is easy to convert between hex and binary and fairly easy to go between hex and denary. Remember that the microprocessor only works in binary, all the conversions between hex and binary are carried out in other circuits (Figure 3.1).
Figure 3.1 Hex is a good compromise
Converting denary to hex
The process follows the same pattern as we saw in the denary to binary conversion.
Method
1 Write down the denary number.
2 Divide it by 16 10, put the whole number part of the answer underneath and the remainder in the column to the right.
3 Keep going until the number being divided reaches zero.
4 Read the answer from the bottom to top of the remainders column.
REMEMBER TO WRITE THE REMAINDERS IN HEX.
A worked example
Convert the denary number 23 823 to hex
1 Write down the number to be converted
23 823
(OK so far).
2 Divide by 16. You will need a calculator. The answer is 1488.9375. The 1488 can be placed under the number being converted
23 823
1488
but there is the problem of the decimal part. It is 0.9375 and this is actually 0.9375 of 16. Multiply 0.9375 by 16 and the result is 15. Remember that this 15 needs to be written as a hex number – in this case F. When completed, this step looks like:
23 823
1488 F
3 Repeat the process by dividing the 1488 by 16 to give 93.0 There is no remainder so we can just enter the result as 93 with a zero in the remainder column.
23 823
1488 F
93 0
4 And once again, 93 divided by 16 is 5.8125. We enter the 5 under the 93 and then multiply the 0.8125 by 16 to give 13 or D in hex
23 823
1488 F
93 0
5 D
5 This one is easy. Divide the 5 by 16 to get 0.3125. The answer has now reached zero and 0.3125×16=5. Enter the values in the normal columns to give:
23 823
1488 F = 5D0F
93 0 ↑
5 D ↑
0 5
6 Read the hex number from the bottom upwards: 5D0FH (remember that the ‘H’ just means a hex number).
Example
Convert 44 256 10into hex
44 256
2766 0 = ACE0H
172 E ↑
10 C
0 A
A further example
Convert 540 709 10to hex
540 709
33 794 5 = 84025H
2112 2 ↑
132 0
8 4
0 8
So 540 709 10=84025H but, especially when the hex number does not contain any letters, be careful to include the base of the numbers otherwise life can become really confusing.
Converting hex to denary
To do this, we can use a similar method to the one we used to change binary to denary.
Example
Convert A40E5H to denary
1 Each column increases by 16 times as we move towards the righthand side so the column values are:
16 4 |
16 3 |
16 2 |
16 1 |
16 0 |
65536 |
4096 |
256 |
16 |
1 |
2 Simply enter the hex number using the columns
65536 |
4096 |
256 |
16 |
1 |
A |
4 |
0 |
E |
5 |
3 Use your calculator to find the denary value of each column
65536 |
4096 |
256 |
16 |
1 |
A |
4 |
0 |
E |
5 |
655360 |
16384 |
0 |
224 |
5 |
The left-hand column has a hex value of 10 10(A=10) so the column value is 65536×10=655360. The next column is 4×4096=16384. The next column value is zero (256×0). The fourth column has a total value of 16×14=224 (E = 14). The last column is easy. It is just 1×5=5 no calculator needed!
4 Add up all the denary values:
655 360 + 16 384 + 0 + 224 + 5 = 671 973 10
Method
1 Write down the column values using a calculator. Starting on with 16 0(=1) on the right-hand side and increasing by 16 times in each column towards the left.
2 Enter the hex numbers in the appropriate column, converting them into denary numbers as necessary. This means, for example, that we should write 10 to replace an ‘A’ in the original number.
3 Multiply these denary numbers by the number at the column header to provide a column total.
4 Add all the column totals to obtain the denary equivalent.
Another example
Convert 4BF0H to denary
16 3 |
16 2 |
16 1 |
16 0 |
column values |
4096 |
256 |
16 |
1 |
column values |
4 |
11 |
15 |
0 |
hex values |
16 384 |
2816 |
240 |
0 |
denary column totals |
Total = 16 384 + 2816 + 240 + 0 = 19 440 10
Converting binary to hex
This is very easy. Four binary bits can have minimum and maximum values of 0000 2up to 1111 2. Converting this into denary by putting in the column headers of: 8, 4, 2 and 1 results in a minimum value of 0 and a maximum value of 15 10. Doesn’t this fit into hex perfectly!
This means that any group of four bits can be translated directly into a single hex digit. Just put 8, 4, 2 and 1 over the group of bits and add up the values wherever a 1 appears in the binary group.
Example
Convert 100000010101011 2to hex
Step 1 Starting from the right-hand end, chop the binary number into groups of four.
100/ 0000/ 1010/ 1011/
Step 2 Treat each group of four bits as a separate entity. The right-hand group is 1011 so this will convert to:
8 |
4 |
2 |
1 |
column headers |
1 |
0 |
1 |
1 |
binary number |
8 |
0 |
2 |
1 |
column values |
The total will then be 8 + 0 + 2 + 1 = 1110 or in hex, B. The right-hand side binary group can now be replaced by the hex value B.
Step 3 The second group can be treated in the same manner. The bits are 1010 and by comparing them with the 8, 4, 2, 1 header values this means the total value is (8×1)+(4×0)+(2×1)+(1×1) = 8 + 0 + 2 + 0 = 1010 or in hex, A.
We have now completed two of the groups.
100/ |
0000/ |
1010/ |
1011/ |
|
|
A |
B |
Step 4 The next group consists of all zeros so we can go straight to an answer of zero. The result so far will be:
100/ |
0000/ |
1010/ |
1011/ |
|
0 |
A |
B |
Step 5 The last group is incomplete so only the column headings of 4, 2, and 1 are used. In this case, the 4 is counted but the 2 and the 1 are ignored because of the zeros. This gives a final result of:
100/ |
0000/ |
1010/ |
1011/ |
4 |
0 |
A |
B |
So, 100000010101011 2= 40ABH.
Having chopped up the binary number into groups of four the process is the same regardless of the length of the number. Always remember to start chopping from the right-hand side.
Example
Convert the number 1100011111001 2to hex
Split it into groups of four starting from the right-hand side
1/ 1000/ 1111/ 1001/
Add column headers of 8 4 2 1 to each group
1 |
8421 |
8421 |
8421 |
column headings |
1/ |
1000/ |
1111/ |
1001 |
binary number |
1 |
8 |
8421 |
81 |
column values |
1 |
8 |
15 |
9 |
group value in denary |
Now just convert group values to hex as necessary. In this example only the second group 15, will need changing to F. Final result is 1100011111001 2=18F9H.
Читать дальше