Hexadecimal Numbers

Hexadecimal (hex) offers a nice, compact way to represent binary numbers.

Using four bits to represent hex digits: logic.ly

When you see the hexadecimal prefix (0x), know that you're looking at a hex number - even though you're not seeing the zeroes and ones.

Example: 0x24 does not equal (24)10.

Convert: Binary to Hex

Convert the binary number to hexadecimal: (11011100)2

  1. Break binary number into groups of 4 bits:
  1. Convert each nibble to decimal:
  1. Convert each decimal nibbles to hex:
  1. Concatenate the results (don't forget the 0x prefix!):

0xDC

Convert: Hex to Binary

For hex to binary, we simply go the other direction - take each hex digit and convert it its binary equivalent.

Example 1

Convert the hexadecimal number to binary: 0x24

When we see 0x24, we're looking at a byte (8 bits):

2 4
? ? ? ? ? ? ? ?

So we convert to binary...

2 = (0010)2

4 = (0100)2

Then plug in the result:

2 4
0 0 1 0 0 1 0 0

Final result = 00100100

Example 2

Convert the hexadecimal number to binary: 0xA1

Nothing different here, we just have to know that the letters represent numbers.

A = (10)10 = (1010)2

1 = (1)10 = (0001)2

Plugging in:

A 1
1 0 1 0 0 0 0 1

Final result = 10100001