ip2bin-en-bin2ip
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
function decextbin($dec, $bits = null){
$bin = decbin($dec); // convert
if(!is_null($bits) && strlen($bin) > $bits) throw new LengthException('The resulting binary is more than '.$bits.' bits.');
else while(strlen($bin) < $bits) $bin = '0'.$bin; // add zero's
return $bin; //done
}
function ip2bin($ip){
return decextbin(ip2long($ip),32);
}
function bin2ip($bin){
return long2ip(bindec($bin));
}
// voorbeeldje
var_dump( ip2bin('127.0.0.1'), bin2ip('01111111000000000000000000000001'), decextbin(45,8) );
?>
function decextbin($dec, $bits = null){
$bin = decbin($dec); // convert
if(!is_null($bits) && strlen($bin) > $bits) throw new LengthException('The resulting binary is more than '.$bits.' bits.');
else while(strlen($bin) < $bits) $bin = '0'.$bin; // add zero's
return $bin; //done
}
function ip2bin($ip){
return decextbin(ip2long($ip),32);
}
function bin2ip($bin){
return long2ip(bindec($bin));
}
// voorbeeldje
var_dump( ip2bin('127.0.0.1'), bin2ip('01111111000000000000000000000001'), decextbin(45,8) );
?>
Levert: