I have written the following function to convert an IP address in binary format to decimal dotted notation:
function bin_to_ddn($binary_ip) #converts a binary IP in DDN
{
$binary_ip_arr = str_split($binary_ip,8); #convert binary IP to array
foreach($binary_ip_arr as &$value) { #convert each octet to decimal
$value = bindec($value);
}
$ddn = implode('.',$binary_ip_arr); #convert to string
return $ddn;
}
The problem is that this function works for some numbers and not for others. For example:
11000111000000010000000101100000
becomes
199.1.1.96
But
11000111000000010000000101111111
becomes
199.1.1.127.0
For some reason, it's adding an extra octet at the end. Any idea what might be wrong in this function?
$binary_ip_arrcontains 5 elements. Are you sure there are no leading or trailing spaces in$binary_ip?199.1.1.127forecho bin_to_ddn("11000111000000010000000101111111");)