I thought that I should write this post after I began discussing a method to covert between these base 10, base 2, and base 16 methods of conveying numbers; and since Phil and I were talking about this today, I figured the information would possibly be of use for some people out there that deal with IP’s, subnetting, and even making sense of IPv6 addresses. I am going to take only a slightly mathematical approach, and gear this for the completely practical or more visual mind.
Let’s say that you have the IP address of 192.168.20.1 and you need to represent this in binary for some reason. How would you do this? First you should understand that what you are looking at in the dotted decimal notation is bytes or rather 8 bits. Secondly, it is important to consider that each bit has a given value that can be either on or off.
The left-most bit is the most significant bit (or has the highest value), so we could think of the bits as having the following values: 128, 64, 32, 16, 8, 4, 2, 0. In this list we have 8 different bits and combinations of these bits can be on to form a total possible value of 255. If we want to represent the IP address of 192.168.20.1, we can simplify our task by working through each each bits, one at a time.
192 = 11000000
168 = 10101000
20 = 00010100
1 = 00000001
You could write the entire IP in binary form as: 11000000.10101000.00010100.00000001
Then you may want to convert to Hexidecimal, this is very easy to do and it similar to the approach that we just took in converting to binary form. However a notable difference with Hexidecimal is that it is a base 16 system, so the values look like this: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. You can think of a mapping relationship as A=10, B=11, … , and F=15.
If we want to convert the first octet (8 bits) with the value 192 to hexidecimal, you can do the following: Take the binary value of ‘11000000′ and split it into two equal parts of 4 bits. For example: 1100 and 0000. Now you can associate bit values to each bit just like we did when converting to decimal. For the set of ‘0000′ we can safely assume that the value is ‘0′. For the set of ‘1100′ we can say that the most significant bit is worth a value of ‘8′, the second bit is ‘4′, so we sum these and get the value of ‘12′. Now in hex the decimal value of ‘12′ is ‘C’.
We have ‘C’ and ‘0′, so we can write this as ‘0xC0′. This value is the same as decimal value 192.
Now onto some slightly harder stuff. Lets take the IPV6 address of : 2001:470:8859:beef:21f:f3ff:fed2:d571, which just happens to exist on my network. Let’s say that for some application programming that I am working on requires that I convert the network value of ‘2001:470:8859:beef’ to binary notation. Here is what I would do:
2001 = 20 01
20 = 00100000
01 = 00000001
04 = 00000100
70 = 01110000
88 = 10001000
59 = 01011001
BE = 10111110
EF = 11101111
Put that all together and you have:
0010000000000001:0000010001110000:1000100001110000:1000100001011001:101111101110111
Easy as pie.