Converting between hex and binary
Introduction
In the previous section, we saw how to convert between decimal numbers and hex. We did this by using a trick to do the conversion. We converted our decimal number into binary first and then into hex. We are using the fact that a hex number is simply a group of 4 binary bits (and a group of 4 bits is called a nibble) .
To convert a binary number into hex, use these weightings for each bit in the nibble:
Worth of each bit position in the nibble |
8 |
4 |
2 |
1 |
Example 1 - convert 0010 1010 into hex.
-
- Each group of four bits is a hex number.
- 0010 is 2 in denary, which is 2 in hex.
- 1010 is 10 in denary, which is A in hex.
- Putting the hex digits together, we have 0010 1010 in binary is 2A in hex.
Example 2 - convert 0011 1110 into hex.
-
- Each group of four bits is a hex number.
- 0011 is 3 in denary, which is 3 in hex.
- 1110 is 14 in denary, which is E in hex.
- Putting the hex digits together, we have 0011 1110 in binary is 3E in hex.
Example 3 - convert 0110 0000 into hex.
-
- Each group of four bits is a hex number.
- 0110 is 6 in denary, which is 6 in hex.
- 0000 is 0 in denary, which is 0 in hex.
- Putting the hex digits together, we have 0110 0000 in binary is 60 in hex.
Example 4 - convert 1111 0100 into hex.
-
- Each group of four bits is a hex number.
- 1111 is 15 in denary, which is F in hex.
- 0100 is 4 in denary, which is 4 in hex.
- Putting the hex digits together, we have 1111 0100 in binary is F4 in hex.
To convert a hex number into binary, we just reverse the process. Treat each hex digit as its own 4 bit number.
Example 1 - convert 2B16 into binary.
-
- Each hex digit is a standalone nibble.
- 2 in hex is 0010 in binary.
- B in hex is 1011 in binary.
- Putting the nibbles together, we have 2B in hex is 0010 1011 in binary.
Example 2 - convert FF16 into binary.
-
- Each hex digit is a standalone nibble.
- F in hex is 1111 in binary.
- F in hex is 1111 in binary.
- Putting the nibbles together, we have FF in hex is 1111 1111 in binary.
Example 3 - convert 0D16 into binary.
-
- Each hex digit is a standalone nibble.
- 0 in hex is 0000 in binary.
- D in hex is 1101 in binary.
- Putting the nibbles together, we have 0D in hex is 0000 1101 in binary.
Example 4 - convert 4716 into binary.
-
- Each hex digit is a standalone nibble.
- 4 in hex is 0100 in binary.
- 7 in hex is 0111 in binary.
- Putting the nibbles together, we have 47 in hex is 0100 0111 in binary.