Книга: Introduction to Microprocessors and Microcontrollers
Converting binary to hex
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 |
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, 1000000101010112 = 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 11000111110012 to 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 11000111110012=18F9H.
- 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