Tuesday, January 8, 2013

TIL: IP Decimal to Hex conversion

One thing that recently threw me for a loop was the conversion of IP addresses from decimal to hex or vice versa.  I personally don't do this on a daily basis, but discovered via my CCIE studies that this is assumed knowledge for the test and lab.

The reason this knowledge is needed is for configuring DHCP option 43 on Cisco IOS.  All option 43 does is get the IP of your WLC to your AP thru DHCP services.  The Cisco way is to set your option 43 address(es) in hex via the IOS CLI.  In IOS the option 43 command uses a leading prefix prior to adding the address(es).  This prefix is a 0xf1, however, slapping 0xf1 in front of your hex address(es) just wont do.  The reason being is the prefix is used to tell the AP how many addresses it's receiving.
Thus the prefix changes based on the number of addresses being specified.  Rest assured the forumla for this is not terribly complicated, in fact it's extremely simple.  

It is as follows:

 # of WLC IP addresses * 4

For example, if 1 WLC is being specified the leading prefix would be f104.  If 2 WLC addresses were being specified then the prefix would be f108.  The 0x is not needed when entering the command into the CLI.

Now the basic structure is laid out for configuring option 43.  Next we need to take our two WLC IP addresses, 192.168.1.10 and 192.168.1.11 and translate them to hex.

At first this seemed like a monumental task to me, as I figured have to endure hours of reading and trying to understand formulas and conversion tables.  But alas, this too is simple, as long as you know the hex to binary/decimal mappings.  Let's review it below.



The key here is to remember that the IP address is made up of 4 octets.  If you recall an octet is 8 bits.  A hex number is comprised of 4 bits.  So you need 2 hex numbers to make 1 octet.

Let me put the IP's in a table representing the binary expression.

192.168.1.10
128
64
32
16
8
4
2
1

1
1
0
0
0
0
0
0
=192
1
0
1
0
1
0
0
0
=168
0
0
0
0
0
0
0
1
=1
0
0
0
0
1
0
1
0
=10

Now I can clearly see my hex address for 192.168.1.10 will be C0A8010A.


192  =  1100 0000    =  C 0
168  =  1010 1000    =  A 8
1      =   0000 0001   =  0 1
10    =   0000 1010   =  0 A

192.168.1.11
128
64
32
16
8
4
2
1

1
1
0
0
0
0
0
0
=192
1
0
1
0
1
0
0
0
=168
0
0
0
0
0
0
0
1
=1
0
0
0
0
1
0
1
1
=11


Again I can clearly see my hex address for 192.168.1.11 will be C0A8010B

192  =  1100 0000    =  C 0
168  =  1010 1000    =  A 8
1      =   0000 0001   =  0 1
11    =   0000 1010   =  0 B


Now back to my original reason for figuring this all out, IOS Option 43.  In your DHCP pool add the following command

(dhcp-config)#option 43 hex f108C0A8010BC0A8010A

There, that wasn't so hard(at least that's what I told myself)?  The last step is to verify your AP gets the address via option 43 and connects to the WLC.  That is really outside the scope of what I was trying to figure out here, so I'll leave that part out.  

If this still seems confusing take the above tables and blank them out and create your own addresses and practice the conversion.  Or better yet go backwards!  This was the only way I was able to learn subnetting and ultimately figure this out.  With any skill, practice makes perfect!


-Travis