IP addresses are 32-bit binary numbers, but because humans would find it inconvenient to write down 32-bit numbers, the addresses are written in decimal. The format of decimal IP addresses is often called canonical format, and sometimes called dotted decimal format. For example, the next two lines show the binary version of an IP address, followed by the same IP address written as a dotted decimal number. It's obvious from comparing the two that, given a choice, it's typically much easier to work with the decimal version:
The next two sections cover the conversion process for IP addresses, first from decimal to binary, and then vice versa. Converting Decimal IP Addresses to Binary IP AddressesYou have already read about the math behind the conversion process between decimal and binary. To convert IP addresses, you simply need to follow a few additional rules:
In other words, to convert a decimal IP address to its 32-bit binary equivalent, you would convert each of the 4 decimal numbers in a decimal IP address to a binary number. You might recall from Chapter 10 that each of the 4 numbers in a decimal IP address, separated by periods, is called an octet. Essentially, you convert each of the 4 decimal octets to 8-bit binary numbers. For any of these binary numbers that converted to less than 8 digits, you would put binary 0s to the left to make them 8 digits long. Finally, just think of the 4 octets as one long 32-bit number, and you're done! Table B-15 shows a sample conversion of the IP address 100.235.2.2.
Table B-15 begins with the decimal IP address in the first row, and the results of each of the three conversion steps in the next three rows. The actual math for converting the decimal numbers (Step 1) is not shown, but you can refer to the previous section for examples using decimal 100 and 235. For the first, third, and fourth octets, the converted binary numbers are less than 8 bits long, so at Step 2, binary 0s were added to the left to make them all 8 bits long. Step 3 just lists all 32 bits in succession. In real life, there's no need to actually write down Step 3; you can just see the 4 sets of 8 bits all in a row and think of it as a 32-bit number. Converting Binary IP Addresses to Decimal IP AddressesTo convert from a binary IP address to its decimal equivalent, you already know the 32-bit IP address. The process is rather simple compared to converting from decimal to binary:
The algorithm can be shown with a sample binary value01100100111010110000000100000001. Table B-16 organizes the bits into octets with 8 bits each.
The resulting IP address, with the periods added, would be 100.235.1.1. The math used for the conversion was covered in the section titled "Converting from Binary to Decimal" earlier in this appendix. But the conversion process is not the tricky part. When you start with a 32-bit number, as long as you organize it into 4 sets of 8 bits (4 binary octets), you can go through the math to convert each 8-bit binary to decimal. However, if you misstep and convert a 9-bit binary to decimal and then a 7-bit binary number to decimal, you'll be converting the number incorrectly. Using a Conversion ChartNow you understand the basics of how decimal and binary work and how to convert between the two. You also know a few rules you must follow when converting IP addresses between the two formats. You can always use a calculator to do the math of converting a decimal number to binary, or vice versa. Because IP addresses only use decimal numbers between 0 and 255, you can also use a binary/decimal conversion chart. A binary/decimal conversion chart simply lists decimal numbers along with their binary equivalents. That way, you can look in the chart and find the numbers without using all the math covered earlier in this appendix. For instance, to convert 100.235.1.1 to binary, you could look in the chart and find the decimal number 100. Beside it, you would find the 8-bit binary number 01100100. You would simply write those digits down as the first 8 binary digits. Next, you would find 235 in the chart, find the binary value beside itnamely 11101011and write that down. You can also use the chart to convert from binary IP addresses to decimal. You follow the same algorithm, but instead of doing the math, you find the 8-bit binary number in the chart and record the decimal number beside it as the value of the IP address in that octet. Table B-17 lists a binary/decimal conversion chart for your reference. Note that all the binary numbers are shown as 8-digit numbers, because when they're used for converting IP addresses, you will want a full 8 bits for each octet.
|