<?
/**
* Given a Hex IP Address, this script will convert
* and display Decimal and Binary representations of
* the IP Address.
*
* This script was created for a one time personal use.
* It works for well for valid hex addresses, but may return
* unexpected results for invalid hex input (non hex characters,
* too many/few hex digits, etc).
*
* @author Josh Grochowski (josh@kastang.com)
*
*/
//determines if the input is through a command line, or web browser
if(isset($argv[1])) {
$HEX_IP = $argv[1];
} else if(isset($_GET["hexip"])) {
$HEX_IP = $_GET["hexip"];
} else {
throw new Exception("No HEX Input through CLI or GET.");
}
$HEX_IP = str_split($HEX_IP, 2);
$BIN_IP = '';
foreach($HEX_IP as $h) {
$d = base_convert($h, 16, 2);
//Pads the Binary string with 0's if length is less then 8.
if(strlen($d) < 8) {
$len = strlen($d);
while($len < 8) {
$d = '0'.$d;
$len++;
}
}
$BIN_IP .= $d;
}
//Displays the length of the Binary string and shows the IP in Binary Notation
echo 'Binary Length: '. strlen($BIN_IP)."\n";
echo $BIN_IP."\n\n";
//Splits the string into 4, 8 bit segments.
$BIN_IP = str_split($BIN_IP, 8);
//Converts each Binary block to a decimal and forms the IP address.
echo 'Decimal IP: ';
$ip = '';
foreach($BIN_IP as $x) {
$ip .= base_convert($x, 2, 10).'.';
}
//Displays the IP Address in Decimal format.
echo substr($ip, 0, -1)."\n";
?>
<?
/**
* Given a Hex IP Address, this script will convert
* and display Decimal and Binary representations of
* the IP Address.
*
* This script was created for a one time personal use.
* It works for well for valid hex addresses, but may return
* unexpected results for invalid hex input (non hex characters,
* too many/few hex digits, etc).
*
* @author Josh Grochowski (josh@kastang.com)
*
*/
//determines if the input is through a command line, or web browser
if(isset($argv[1])) {
$HEX_IP = $argv[1];
} else if(isset($_GET["hexip"])) {
$HEX_IP = $_GET["hexip"];
} else {
throw new Exception("No HEX Input through CLI or GET.");
}
$HEX_IP = str_split($HEX_IP, 2);
$BIN_IP = '';
foreach($HEX_IP as $h) {
$d = base_convert($h, 16, 2);
//Pads the Binary string with 0's if length is less then 8.
if(strlen($d) < 8) {
$len = strlen($d);
while($len < 8) {
$d = '0'.$d;
$len++;
}
}
$BIN_IP .= $d;
}
//Displays the length of the Binary string and shows the IP in Binary Notation
echo 'Binary Length: '. strlen($BIN_IP)."\n";
echo $BIN_IP."\n\n";
//Splits the string into 4, 8 bit segments.
$BIN_IP = str_split($BIN_IP, 8);
//Converts each Binary block to a decimal and forms the IP address.
echo 'Decimal IP: ';
$ip = '';
foreach($BIN_IP as $x) {
$ip .= base_convert($x, 2, 10).'.';
}
//Displays the IP Address in Decimal format.
echo substr($ip, 0, -1)."\n";
?>
Fatal error: Uncaught Exception: No HEX Input through CLI or GET. in /in/hX9IH:22
Stack trace:
#0 {main}
thrown in /in/hX9IH on line 22
Process exited with code 255.
Fatal error: Uncaught exception 'Exception' with message 'No HEX Input through CLI or GET.' in /in/hX9IH:22
Stack trace:
#0 {main}
thrown in /in/hX9IH on line 22
Process exited with code 255.