Книга: Introduction to Microprocessors and Microcontrollers

Converting hex to binary

Converting hex to binary

This is just the reverse of the last process. Simply take each hex number and express it as a four bit binary number.

As we saw in the last section, a four-bit number has column header values of 8, 4, 2 and 1, so conversion is just a matter of using these values to build up the required value. All columns used are given a value of 1 in binary and all unused columns are left as zero. When you are converting small numbers like 3H we must remember to add zeros on the left-hand side to make sure that each hex digit becomes a group of four bits.

Imagine that we would like to convert 5H to binary. Looking at the column header values of 8, 4, 2 and 1, how can we make the value 5?

The answer is to add a 4 and a 1. Taking each column in turn: we do not need to use an 8 so the first column is a 0. We do want a 4 so this is selected by putting a 1 in this column, no 2 so make this 0 and finally put a 1 in the last column to select the value of 1. The 5H is converted to 01012. All values between 0 and FH are converted in a similar way.

Example

Convert 2F6CH to binary

Step 1 Write the whole hex number out with enough space to be able to put the binary figures underneath

2 F 6 C

Step 2 Put the column header values below each hex digit

2 F 6 C
8421 8421 8421 8421

Step 3 The hex C is 1210 that can be made of 8+4 so we put a binary 1 in the 8 and the 4 columns. The four-bit number is now 11002

2 F 6 C
8421 8421 8421 8421
      1100

Step 4 Now do the same for the next column. The hex number is 6, which is made of 4+2, which are the middle two columns. This will result in the binary group 01102

2 F 6 C
8421 8421 8421 8421
    0110 1100

Step 5 Since 8+4+2+1 = 15, the hex F will become 11112

2 F 6 C
8421 8421 8421 8421
  1111 0110 1100

Step 6 Finally, the last digit is 2 and since this corresponds to the value of the second column it will be written as 00102

2 F 6 C
8421 8421 8421 8421
0010 1111 0110 1100

The final result is 2F6CH = 00101111011011002. But do we include the two leading zeros? There are two answers, ‘yes’ and ‘no’ but that’s not very helpful. We need to ask another question: why did we do the conversion? were we doing math or microprocessors? If we were working on a microprocessor system then the resulting 16 bits would represent 16 voltages being carried on 16 wires. As the numbers change, all the wires must be able to switch between 0 V and 3.3 V for binary levels 0 and 1. This means, of course, that all 16 wires must present so we should include the binary levels on all of them.

If the conversion was purely mathematical, then since leading (lefthand end) zeros have no mathematical value there is no point in including them in the answer.

Method

1 Write down the hex number but make it well spaced.

2 Using the column header values of 8, 4, 2 and 1, convert each hex number to a four bit binary number.

3 Add leading zeros to ensure that every hex digit is represented by four bits.

Example

Convert 1E08BH to binary

Step 1

1 E 0 8 B
8421 8421 8421 8421 8421

Step 2

0001 1110 0000 1000 1011

So, 1E08BH = 000111100000100010112.

Using stepping stones

It is fairly easy to convert binary to hex and hex to binary. I find it much easier to multiply and divide by 2 rather than by 16, so when faced with changing hex into denary and denary into hex I often change them into binary first. It is a longer route but at least I can do it without my calculator (see Figure 3.2).


Figure 3.2 A longer route may prove easier

Оглавление книги


Генерация: 0.531. Запросов К БД/Cache: 2 / 0
поделиться
Вверх Вниз