Frage

I want to convert an IP address or subnet mask to bits.
Is there an easy way to do so?
Example:
Input: 255.255.255.0
Output: 11111111 11111111 11111111 00000000

War es hilfreich?

Lösung

ip2long is very useful in this case:

$ipAddress = "8.8.8.8";
echo decbin(ip2long($ipAddress));

Also, note that decbin() won't always output the same number of digits, so try using sprintf() instead to always get 32 bits:

sprintf("%032b", ip2long($ipAddress));

Andere Tipps

Try this code i made it for you:



$ipAddress = "255.255.255.0";

$ipsArray = explode(".", $ipAddress);

foreach($ipsArray as $ip){
    $ipInt = (int)$ip;

    echo decbin($ipInt) . " "; 
}


Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top