How Are Your Hexadecimal Skills?

Working with IPv6 addresses means working with hex. It's easier than you might think.

I ended my post on IPv6 address design saying I would follow up by showing you a few simple tricks for working with hexadecimal numbers. Then the long-predicted depletion of the IANA pool of IPv4 addresses happened, I got distracted, and wrote a couple of posts on that. But now it’s time to make good on my promise; better late than never.

Dotted decimal is well suited to representing IPv4 addresses; its 32 bits are broken up into four 8-bit octets, and then each of those octets are written in their decimal equivalent –some decimal number between 0 and 255 – and separated by dots.

A 128-bit address is a different matter. In dotted decimal, an IPv6 address might look like:

            32.1.13.184.35.24.105.196.131.84.126.3.218.92.171.205

It’s long, it’s more difficult to read, and the dotted notation makes it easy to accidentally jumble digits as you read it. It’s hard on the eyes.

Rather than breaking the 128-bit IPv6 address up into sixteen octets and writing each octet as a decimal number separated by dots, let’s break the same address up into eight 16-bit segments and write each segment as a hexadecimal number separated by colons:

            2001:0db8:2318:69c4:8354:7e03:da5c:abcd

Admittedly it’s still not as tidy as an IPv4 address, but it’s easier to read than its dotted decimal form. It’s shorter, the larger variation in digits (16 rather than 10) makes it easier to read, and the clearer separation of the colons reduces the “jumble factor.”

Just for fun, the IPv4 address 192.168.213.84 written in IPv6-style hexadecimal notation is c0a8:d554. The address might look foreign and intimidating to you if you haven’t worked with hex before, but once you get used to it you’ll find working with hex easier than working with dotted decimal. In particular, it’s easier to convert between hex and binary than between decimal and binary.

The reason for this is that each hexadecimal digit has 16 possible values (0 – f), corresponding to the 16 potential values of a 4-bit binary number. It’s very easy to remember 16 values or quickly work them out in your head:

 
Hex Digit Decimal Equivalent Binary Equivalent

0

0

0000

1

1

0001

2

2

0010

3

3

0011

4

4

0100

5

5

0101

6

6

0110

7

7

0111

8

8

1000

9

9

1001

a

10

1010

b

11

1011

c

12

1100

d

13

1101

e

14

1110

f

15

1111

A decimal digit, on the other hand, has only 10 possible values (0 – 9), so there is no one-to-one correspondence between decimal digits and any group of binary digits. Instead we choose to decimally represent the 256 possible values in an octet because computers and routers regularly work with octets (bytes). It’s difficult to remember 256 different values, so we resort to a bit of arithmetic to convert between decimal and binary.

For example, take the decimal number 213. If we want to find its binary equivalent, we first have to know the place values of a binary octet, which from left to right are:

            128    64    32    16    8    4    2    1

We start out with all eight place values unknown:

             xxxxxxxx

213 is larger than 128, so we write a 1 in the “128” place:

            1xxxxxxx

213-128 = 85, which is larger than 64, so we write a 1 in the “64” place:

            11xxxxxx

85-64 = 21. That’s smaller than 32, so we write a 0 in that place; it’s larger than 16, so we write a 1 in that place:

            1101xxxx

21-16 = 5. That’s smaller than 8, so we write a 0 in the “8” place; it’s larger than 4, so we write a 1 in the “4” place:

            110101xx

5-4 = 1, so there is a 0 in the “2” place and a 1 in the “1” place, and we’re done:

            213 = 11010101

Converting from binary to decimal uses a similar sequence of simple arithmetical steps, except we add instead of subtract. Take the following octet:

            10011010

Knowing the place values, we can see that there are 1’s in the 128, 16, 8, and 2 places. 128+16+8+2 = 154. So:

            10011010 = 154

These conversions are easy and can be quickly worked out on a piece of paper. But look how much easier it is to convert between hexadecimal and binary. Take the hex number 8c (every possible value of a binary octet can be represented with two hex digits):

            You know without the aid of paper that the binary equivalent of 8 is 1000

            You know that the binary equivalent of c (decimal 12) is 1100.

So just put those to values together, and you have your conversion:

             8c = 10001100

It’s just as easy to convert in the other direction. Take the binary octet 01110101. Just break it into two 4-bit numbers, 0111 and 0101:

            0111 = 7

            0101 = 5

So:

             01110101 = 75

You don’t even have to memorize the sixteen binary numbers corresponding to the sixteen hex digits; you only have to know that the four place values are 8, 4, 2, and 1. So, for example, 0101 is 4+1=5. 1110 is 8+4+2=e (decimal 14).

IPv6 addresses represent 16 bits, or 2 octets, between each colon. Conversions are just as easy. Take the example address used earlier in this article:

            2001:0db8:2318:69c4:8354:7e03:da5c:abcd

Let’ say you need to see the fourth segment of that address, 69c4, in binary:

            6 = 0110

            9 = 1001

            c = 1100

            4 = 0100

Sticking it all together:

            69c4 = 0110100111000100

Or perhaps you’re working with a binary portion of an address to visualize an address design, and need to convert the result back to hex. Let’s use the following 16-bit number:

            1111000011001100

Again, just break it up into 4-bit parts, find the hex equivalent of each part, and put the hex digits together:

            1111 = f  (that one is certainly easy)

            0000 = 0  (no kidding!)

            1100 = c

            1100 = c

So:

            1111000011001100 = f0cc.

Every once in a while you’ll need to convert between decimal and hex, such as when working with a tunneling protocol that embeds an IPv4 address within an IPv6 address. If you don’t have a scientific calculator handy, you can still do quick conversions on a bit of scratch paper, using what you already know: Convert from decimal to binary and then to hex, or from hex to binary and then to decimal. The conversions between hex and binary are so easy, there is no need to add any more tricks to your bag.

As you enable IPv6 in your network, your engineers and network operators are going to need to be able to do conversions. Using these techniques, it’s something you can teach them in a quick 20-minute whiteboard session.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.
Must read: 10 new UI features coming to Windows 10