## Книга: 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 0101_{2}. 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 12_{10} 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 1100_{2}

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 0110_{2}

2 | F | 6 | C |
---|---|---|---|

8421 | 8421 | 8421 | 8421 |

0110 | 1100 |

Step 5 Since 8+4+2+1 = 15, the hex F will become 1111_{2}

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 0010_{2}

2 | F | 6 | C |
---|---|---|---|

8421 | 8421 | 8421 | 8421 |

0010 | 1111 | 0110 | 1100 |

The final result is 2F6CH = 0010111101101100_{2}. 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 = 00011110000010001011_{2}.

**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

- Hexadecimal, or ‘hex’ to its friends
- 3. Hexadecimal – the way we communicate with micros
- 1.6 Converting Binary Numbers into Decimal
- 1.7 Converting Decimal Numbers into Binary
- 1.8 Converting Binary Numbers into Hexadecimal
- 1.9 Converting Hexadecimal Numbers into Binary
- 1.10 Converting Hexadecimal Numbers into Decimal
- 1.11 Converting Decimal Numbers into Hexadecimal
- 1.12 Converting Octal Numbers into Decimal
- 1.13 Converting Decimal Numbers into Octal
- 1.14 Converting Octal Numbers into Binary
- 1.15 Converting Binary Numbers into Octal