<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="robots" CONTENT="NOINDEX,NOFOLLOW">
<link rel="stylesheet" type="text/css" href="ip.css">
<title>Eric's IPv4 Address 2 Binary Calculator</title>
</head>
<body>
<h1>Eric's IPv4 Address 2 Binary Calculator</h1><br>
<?php

// Process form if an IP address was supplied
if ((is_numeric($_POST['ip_1'])) && (is_numeric($_POST['ip_2'])) && (is_numeric($_POST['ip_3'])) && (is_numeric($_POST['ip_4']))) {

    
// Create a function to prepend the binary number with zeroes
    
function zerofill ($num) {
        
$zerofill 8;
        while (
strlen($num) < $zerofill) {
            
$num "0" $num;
        }
        return 
$num;
    }

    
// Get the parts of the IP Address
    
$ip1 trim($_POST['ip_1']);
    
$ip2 trim($_POST['ip_2']);
    
$ip3 trim($_POST['ip_3']);
    
$ip4 trim($_POST['ip_4']);

    
// Calculate the binary for each octet
    
$ip1bin zerofill(base_convert($ip1102));
    
$ip2bin zerofill(base_convert($ip2102));
    
$ip3bin zerofill(base_convert($ip3102));
    
$ip4bin zerofill(base_convert($ip4102));

    
// Determine the subnet mask and class of the IP address based on the inital octet
    
if (($ip1 255) || ($ip2 255) || ($ip3 255) || ($ip4 255)) {
        
$class "ERROR: Too large";
        
$subnetmask "ERROR: Too large";
        
$ip1bin "ERROR: Too large";
        
$ip2bin "";
        
$ip3bin "";
        
$ip4bin "";

    } elseif ((
$ip1 >= 0) && ($ip1 <= 127)) {
        
$class "A";
        
$subnetmask "255.0.0.0";

    } elseif ((
$ip1 >= 128) && ($ip1 <= 191)) {
        
$class "B";
        
$subnetmask "255.255.0.0";

    } elseif ((
$ip1 >= 192) && ($ip1 <= 223)) {
        
$class "C";
        
$subnetmask "255.255.255.0";

    } elseif ((
$ip1 >= 224) && ($ip1 <= 247)) {
        
$class "D";

    } elseif ((
$ip1 >= 248) && ($ip1 <= 255)) {
        
$class "E";

    } else {
        
$class "Unknown";
    }

    
// Get the whole IP address; underlining the host bits
    
if ($class == "A") {
        
$ip "<u>" $ip1 "</u>." $ip2 "." $ip3 "." $ip4;
        
$netamount "128 = (2<sup>7</sup>)";
        
$hostamount "16,777,214 = (2<sup>24</sup> - 2)";
        
$firsthost $ip1 ".0.0.1";
        
$lasthost $ip1 ".255.255.254";

    } elseif (
$class == "B") {
        
$ip "<u>" $ip1 "." $ip2 "</u>." $ip3 "." $ip4;
        
$netamount "16,384 = (214)";
        
$hostamount "65,534 = (2<sup>16</sup> - 2)";
        
$firsthost $ip1 "." $ip2 ".0.1";
        
$lasthost $ip1 "." $ip2 ".255.254";

    } elseif (
$class == "C") {
        
$ip "<u>" $ip1 "." $ip2 "." $ip3 "</u>." $ip4;
        
$netamount "2,097,152 = (221)";
        
$hostamount "254 = (2<sup>8</sup> - 2)";
        
$firsthost $ip1 "." $ip2 "." $ip3 ".1";
        
$lasthost $ip1 "." $ip2 "." $ip3 ".254";

    } else {
        
$ip $ip1 "." $ip2 "." $ip3 "." $ip4;
    }

    
// Say so if the address is private
    
if ($ip1 == 10) {
        
$class .= " (Private)";

    } elseif (
$ip1 == 14) {
        
$class .= " (Public data)";

    } elseif ((
$ip1 == 172) && ($ip2 >= 16) && ($ip2 <= 31)) {
        
$class .= " (Private)";

    } elseif ((
$ip1 == 169) && ($ip2 == 254)) {
        
$class .= " (Link-local)";

    } elseif (
$ip1 == 127) {
        
$class .= " (Loopback)";

    } elseif ((
$ip1 == 192) && ($ip2 == 168)) {
        
$class .= " (Private)";
    }

    
// Say so if broadcast
    
if ($ip4 == 255) {
        
$class .= " (Broadcast)";
    }

    
// Show the IP address
    
echo "<b>IP Address</b>:<br>\n$ip<br><br>\n";

    
// Show binary conversion
    
echo "<b>Binary Conversion</b>:<br>\n" $ip1bin " " $ip2bin " " $ip3bin " " $ip4bin "<br><br>\n";

    
// Show the class of the IP address
    
echo "<b>Class</b>:<br>\n$class<br><br>\n";

    
// Show possible amount of networks
    
if (isset($netamount)) {
        echo 
"<b>Possible amount of networks:</b><br>\n$netamount<br><br>\n";
    }

    
// Show possible amount of hosts
    
if (isset($hostamount)) {
        echo 
"<b>Possible amount of hosts per network:</b><br>\n$hostamount<br><br>\n";

    }

    
// Show possible host IPs
    
if ((isset($firsthost)) && (isset($lasthost))) {
        echo 
"<b>Possible host IPs:</b><br>\n$firsthost through $lasthost<br><br>\n";
    }

    
// Show the subnet mask of the IP address, if present
    
if (isset($subnetmask)) {
        echo 
"<b>Subnet Mask</b>:<br>\n$subnetmask<br>\n";
    }

    
// Show a horizontal rule and line break to separate the form
    
echo "<hr align=\"left\"><br>\n";

// End if form was submitted

// Get user's IP address if selected
if ($_GET['fill'] == "local") {
    
$fill "local";
    
$yourip $_SERVER['REMOTE_ADDR'];
    
$youroctet explode("."$yourip);
}

// Show the form and end
?>
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<b>IP Address</b>: <input type="text" maxlength="3" size="3" name="ip_1" <?php if ($fill == "local") { echo "value=\"$youroctet[0]\""; } ?>> . 
<input type="text" maxlength="3" size="3" name="ip_2" <?php if ($fill == "local") { echo "value=\"$youroctet[1]\""; } ?>> . 
<input type="text" maxlength="3" size="3" name="ip_3" <?php if ($fill == "local") { echo "value=\"$youroctet[2]\""; } ?>> . 
<input type="text" maxlength="3" size="3" name="ip_4" <?php if ($fill == "local") { echo "value=\"$youroctet[3]\""; } ?>><br>
<input type="submit" value="Calculate">
</form><br>
&lt; <a href="?fill=local">Fill in</a> &gt; | &lt; <a href="source.phps">View source</a> &gt;<br><br>
<table border="1">

<tr>
<th>Class</th>
<th>First octet in binary</th>
<th>Range of first octet</th>
<th>Network ID</th>
<th>Host ID</th>
<th>Possible number of networks</th>
<th>Possible number of hosts</th>

</tr>
<tr>
<th>A</th>
<td>0XXXXXXX</td>
<td>0 - 127</td>
<td>a</td>
<td>b.c.d</td>
<td>128 = (2<sup>7</sup>)</td>
<td>16,777,214 = (2<sup>24</sup> - 2)</td>

</tr>
<tr>
<th>B</th>
<td>10XXXXXX</td>
<td>128 - 191</td>
<td>a.b</td>
<td>c.d</td>
<td>16,384 = (2<sup>14</sup>)</td>
<td>65,534 = (2<sup>16</sup> - 2)</td>

</tr>
<tr>
<th>C</th>
<td>110XXXXX</td>
<td>192 - 223</td>
<td>a.b.c</td>
<td>d</td>
<td>2,097,152 = (2<sup>21</sup>)</td>
<td>254 = (2<sup>8</sup> - 2)</td>
</tr>

</table>
<div class="cite">IP address. (2009, January 29). In Wikipedia, The Free Encyclopedia. Retrieved 00:52, February 3, 2009, from <a href="http://en.wikipedia.org/w/index.php?title=IP_address&amp;oldid=267228548#IPv4_networks">http://en.wikipedia.org/w/index.php?title=IP_address&amp;oldid=267228548#IPv4_networks</a></div>
</body>
</html>