Выбрать главу

‘One eight’ in hex is equal to twenty-four in denary. Notice how I avoided quoting the hex number as eighteen. Eighteen is a denary number and does not exist in hex. If you read it in this manner it reinforces the fact that it is not a denary value. Here are the main options in order of popularity:

16H = 2410

16H = 2410

16h = 2410

16h = 2410

1616 = 2410

The advantages of hex

1 It is very compact. Using a base of 16 means that the number of digits used to represent a given number is usually fewer than in binary or denary.

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 1610, 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 25610 into hex

44 256

2766 0 = ACE0H

172 E ↑

10 C

0 A

A further example

Convert 540 70910 to hex

540 709

33 794 5 = 84025H

2112 2 ↑

132 0

8 4

0 8

So 540 70910=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:

164 163 162 161 160
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 1010 (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 97310

Method

1 Write down the column values using a calculator. Starting on with 160 (=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

163 162 161 160 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 44010

Converting binary to hex

This is very easy. Four binary bits can have minimum and maximum values of 00002 up to 11112. 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 1510. 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 1000000101010112 to 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.

100/ 0000/ 1010/ 1011/
      B