Hex Digit: | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Bit Group: | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 |
Hex Digit: | 8 | 9 | a | b | c | d | e | f |
Bit Group: | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
For conversion from hex to binary, simply string together the bits for each hex digit. For instance, 0x509d7a is binary 10100001001110101111010. To wit:
Hex Number: | 5 | 0 | 9 | d | 7 | a |
Binary Number: | 0101 | 0000 | 1001 | 1101 | 0111 | 1010 |
To convert the other way, break the binary number into groups of four, then replace each one with its hex digit. Group the digits starting from the right. If you don't have a complete group of four when you reach the left, pad with zero bits on the left to fill the last group. For instance, binary 111011011111110001 is 0x3b7f1:
Binary Groups: | 0011 | 1011 | 0111 | 1111 | 0001 |
Hex Digits: | 3 | b | 7 | f | 1 |
Because this conversion is so easy, the easiest way to convert between binary and decimal is usually to go through hex. It generally requires fewer operations, and hex numbers are easier to work with because they are shorter Also, it's easier to remember where you are when scanning a hex number, since the digits differ more.