Converting numbers from binary into hex isn’t terribly difficult, and the more of it you do, and you’ll do a lot of it the more you work with microcontrollers and digital electronics, the easier it gets to recognize the patterns automatically.

Binary to Hexadecimal Number Conversion

As we have done up to now, let’s start with the number 42, B00101010. Remember, one digit of hex is able to carry the value of four binary digits, so break that binary value into two equal sizes 4 and 4, and examine each in turn: B00101010 becomes 0010 and 1010. The first four bit chunk is, and I’m not making this up, called the “upper nibble” and the second four bit chunk is called the “lower nibble”. Bytes, nibbles and bits…. don’t get me started. Anyway, because a four bit number can only be a value from 0 to 15, it makes the task much easier to accomplish.

0010: is the equivalent of decimal 2. Decimal 2 is 2 in hex.
1010: is the equivalent of decimal 10. Decimal 10 is A in hex.

Now you just combine the chunks together 0010 = 2 and 1010 = A, so combined you have 0x2A. When I look at the nibbles, in my head, I start left to right and go “no eight, no four, a two, no one, equals two” and “an eight, no four, a two, no one, eight and two equals ten.” Then when I go from decimal to hex it’s very easy because it’s a direct mapping that won’t exceed a single hex position. The only tricky part is remembering the A=10, B=11, C=12, D=13, E=14, F=15 pattern, and that’s just a matter of familiarity and practice. You’ll get the hang of it.

Here’s another example, using B11010001

1101: is the equivalent of decimal 13. Decimal 13 is D in hex.
0001: is the equivalent of decimal 1. Decimal 1 is 1 in hex.

1101 = D and 0001 = 1, so combined you have 0xD1.

Here are a few practice problems…

Convert B10010011 to hex… (click here for a description of the solution)

Hover to read answer

Convert B10110101 to hex… (click here for a description of the solution)

Hover to read answer

Convert B11101011 to hex… (click here for a description of the solution)

Hover to read answer

Converting Hexadecimal to Binary

Going in the reverse direction follows a very similar pattern: break the value into discrete chunks, convert the chunks to decimal, convert the decimal to binary. Again, because we break it into upper and lower nibbles, it makes it very easy to work things out. If we want to convert 0x1B to binary, we break it into the two parts 1 and B, then convert each.

1: is the equivalent of decimal 1. Decimal 1 is 0001 in binary.
B: is the equivalent of decimal 11. Decimal 11 is 1011 in binary.

Put the two nibbles of binary together and you get 0x1B = B00011011.

Here’s another example, using 0xCE

C: is the equivalent of decimal 12. Decimal 12 is 1100 in binary.
E: is the equivalent of decimal 14. Decimal 14 is 1110 in binary.

Put the two nibbles of binary together and you get 0xCE = B11001110.

And again, a few practice problems…

Convert 0xD8 to binary… (click here for a description of the solution)

Hover to read answer

Convert 0x53 to binary… (click here for a description of the solution)

Hover to read answer

Convert 0x9F to binary… (click here for a description of the solution)

Hover to read answer

I do not look at 0x2A or 0xD1 and have my brain just read it as “Oh, hey, that’s 42” or “Yep, 209, just like I expected”. I still have to do a mental conversion for most values. As you work more deeply with microcontrollers though, patterns will start becoming very apparent and specific hex numbers will hold important meaning. For example, letters are always higher than numbers so even if you have no idea what the decimal value of 0xD1 vs 0x2A is, you can see how the letters are arranged vs. the numbers: something that starts with a “D” must be bigger than something that starts with a 2. 0xFF and 0x00 will begin to hold a special place in your heart, because the first fills an 8 bit value full of ones and the other fills that same value full of zeros. When you’re working with a 10 bit ADC, and values are streaming in, you’ll begin to recognize 0x3FF as a fully high value, 0x000 as a fully low value and 0x1FF as a voltage half way between fully on and fully off.

The more you work with this, the easier it gets. All it takes is a little practice.

Previous Post
Decimal and Binary Number Conversion
Next Post
Two’s Complement and Negative Binary
You might also like
Menu