How to convert a valid dotted decimal netmask into the corresponding CIDR prefix length
Jan 17th, 2009 by paolo
Yesterday I was googling to find the algorithm to covert a netmask (in the dotted decimal format like 255.255.255.0) in corresponding cidr to manage a database migration, but I can't find it. So I wrote it in PHP.
The following function takes a valid netmask in the dotted decimal format as a string in input and returns the corresponding cidr prefix length as int:
PHP:
-
function netmask2cidr($netmask) {
-
$cidr = 0;
-
for (;$number> 0; $number = ($number <<1) % 256) {
-
$cidr++;
-
}
-
}
-
return $cidr;
-
}

