How to Use Hex with Binary for C Programming - dummies

How to Use Hex with Binary for C Programming

By Dan Gookin

Face it: No one, not even a C programmer, wants to count bits in a binary number. No one. Perhaps some nerd somewhere can tell you that 10110001 is really the value 177, but most programmers can’t. What a good programmer can do, however, is translate binary into hex.

Hex has nothing to do with Harry Potter. It’s short for hexadecimal, which is the base 16 counting system. That’s not as obtuse as it sounds because it’s easy to translate between base 16 (hex) and binary.

For example, the value 10110001 translates into B1 hexadecimal. Hexadecimal numbers include the letters A through F, representing decimal values 10 through 15, respectively. A B in hex is the decimal value 11. Letters are used because they occupy only one character space.

Here are the 16 hexadecimal values 0 through F and how they relate to four bits of data:

Hex Binary Decimal Hex Binary Decimal
0x0 0000 0 0x8 1000 8
0x1 0001 1 0x9 1001 9
0x2 0010 2 0xA 1010 10
0x3 0011 3 0xB 1011 11
0x4 0100 4 0xC 1100 12
0x5 0101 5 0xD 1101 13
0x6 0110 6 0xE 1110 14
0x7 0111 7 0xF 1111 15

These hexadecimal values are prefixed with 0x. This prefix is common in C, although other programming languages may use different prefixes or a postfix.

The next hexadecimal value after 0xF is 0x10. Don’t read it as the number ten, but as “one zero hex.” It’s the value 16 in decimal (base 10). After that, hex keeps counting with 0x11, 0x12, and up through 0x1F and beyond.

Yes, and all of this is just as much fun as learning the ancient Egyptian counting symbols, so where will it get you?

When a programmer sees the binary value 10110100, he first splits it into two 4-bit nibbles: 1011 0100. Then he translates it into hex, 0xB4. The C programming language does the translation as well, as long as you use the %x or %X conversion characters, as shown in A Little Hex.


#include <stdio.h>
char *binbin(int n);
int main()
 int b,x;
 b = 21;
 printf("%s 0x%04X %4dn",binbin(b),b,b);
char *binbin(int n)
 static char bin[17];
 int x;
 bin[x] = n & 0x8000 ? '1' : '0';
 n <<= 1;
 bin[x] = '';

The code in A Little Hex displays a value in binary, hexadecimal, and decimal and then shifts that value to the left, displaying the new value. This process takes place at Line 13 by using the %X conversion character in the printf() statement.

Well, actually, the placeholder is %04X, which displays hex values using uppercase letters, four digits wide, and padded with zeros on the left as needed. The 0x prefix before the conversion character merely displays the output in standard C style.

Exercise 1: Start a new project using the code from A Little Hex. Build and run.

Exercise 2: Change the value of variable b in Line 9 to read this way:

b = 0x11;

Save that change, and build and run.

You can write hex values directly in your code. Prefix the values with 0x, followed by a valid hexadecimal number using either upper- or lowercase letters where required.