When writing programs for microcontrollers we’re usually stuck dealing with 3 different number systems: decimal, binary and hexadecimal (or hex). We use decimal because it comes naturally; that’s the way we count. Unfortunately, it’s not how computers count. Since computers and microcontrollers are limited to 1’s and 0’s, they count using sequences of these numbers. This is the binary number system. Binary numbers are usually prefixed with the '0b' characters which are not part of the number. Sometimes they are also subdivided into groups of 4 digits to make them easier to read as well as easier to relate to the hexadecimal number system. An example of a binary number is 0b0100.1011. The periods in the number don't represent anything, they just make it easier to read the number.

The binary system is simple to understand, but it takes a lot of digits to use the binary system to represent large numbers. The hexadecimal system can represent much larger numbers using fewer characters, and it closely resembles binary numbers. Hexadecimal numbers are usually prefixed with the characters '0x' which are not part of the number. A single hexadecimal digit can represent four binary digits!

Binary numbers can only consist of 1’s and 0’s; typically a binary number consists of 8 digits (or some multiple of 8) if it’s being used in some kind of a computer (or microcontroller). It’s useful to know how to convert a binary number into a decimal number and vice versa. So how do we convert between number systems? First consider how we determine the value of a decimal number. The number 268 can be broken down as 200 + 60 + 8, or 2 * (10^2) + 6 * (10^1) + 8 * (10^0). There are two important numbers that we have to know to ‘deconstruct’ the number - the base of the number system and the location of the digit within the number. The base of a decimal number is 10. When we’re converting the number 268, 2 is the second digit, 6 is the first digit and 8 is the zero digit. Each digit has to be scaled according to its place within the number. The scale of the digit is the base of the number system raised to the power of the digit's location in the number. So each number is scaled, and then all of the scaled digits are added to find the total value of the number.

The same method can be used to find the value of a binary number. For example, let’s look at the number 0b1011.0101. The base of the binary system is 2 (the prefix 0b is often used in code to indicate that the number is in the binary format). The value of our number is: 1*(2^7)+0*(2^6)+1*(2^5)+1*(2^4)+0*(2^3)+1*(2^2)+0*(2^1)+1*(2^0), which is equal to 181.

0b1011.0101. What a completely inefficient way of typing a number! But we can represent the same binary number using only 2 hexadecimal digits. First though, we'll start by converting a hexadecimal (hex) number to decimal like we did for a binary number. How about 0xB5? Wait, what?! The prefix 0x is used in code to indicate that the number is being written in hex. But what is ‘B’ doing in there? The hexadecimal format has a base of 16, which means that each digit can represent up to 16 different values. Unfortunately, we run out of numerical digits after ‘9,’ so we start using letters. The letter ‘A’ represents 10, ‘B’ is 11, ‘C’ is 12, ‘D’ is 13, ‘E’ is 14 and ‘F’ is 15. ‘F’ is the largest digit in the hex numbering system. We convert the number the same way as before. The value of 0xB5, then, is: B*(16^1)+5*(16^0) or 181.

Knowing how to convert binary and hex to decimal is important, but the most useful number conversion is probably converting between hex and binary. These two numbering systems actually work pretty well together. The numbering systems happen to be related such that a single hex digit represents exactly 4 binary digits, and so 2 hex digits can represent 8 bits (or binary digits). Here’s a table that shows how each hex digit is related to the binary system:

Binary Value |
Hex Value |

0000 | 0 |

0001 | 1 |

0010 | 2 |

0011 | 3 |

0100 | 4 |

0101 | 5 |

0110 | 6 |

0111 | 7 |

1000 | 8 |

1001 | 9 |

1010 | A |

1011 | B |

1100 | C |

1101 | D |

1110 | E |

1111 | F |

For example, to convert the hex number 0x1C to binary, we would find the corresponding binary value for 1 and C and combine them. So, 0x1C in binary is 0b0001.1100. If we wanted to figure out the hex value for a binary number we just go the other way. To find the hex representation of the binary number 0b0010.1011 we first find the hex value for 0010, then the hex value for 1011 and combine them; the hex value would be 0x2B.

There are many free tools available to help convert between these numbering systems, just google 'hex number conversion.' If you use Windows as an operating system you have a great tool built into the calculator. Just change the calculator to scientific mode and you can convert between number systems by typing a number then changing the format of the calculator!

What is the decimal representation of the hex number 0xE9?