문제

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

도움이 되었습니까?

해결책

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));

다른 팁

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) . " "; 
}


라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top