1

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?

4
  • if the final output contains 5 elements, then $binary_ip_arr contains 5 elements. Are you sure there are no leading or trailing spaces in $binary_ip? Commented Jan 30, 2015 at 21:38
  • (Also I just tested your code and the output I get is 199.1.1.127 for echo bin_to_ddn("11000111000000010000000101111111");) Commented Jan 30, 2015 at 21:40
  • Way to reinvent the wheel. Commented Jan 30, 2015 at 21:47
  • @AbraCadaver oh well, one can't know them all. It can catch them all though. Commented Jan 30, 2015 at 21:48

3 Answers 3

2

It is very likely that your string contains trailing spaces. Consider the following tests:

echo bin_to_ddn("11000111000000010000000101111111 ");
>>> 199.1.1.127.0

echo bin_to_ddn("11000111000000010000000101111111");
>>> 199.1.1.127

I recommend trimming the input:

$binary_ip_arr = str_split(trim($binary_ip),8);

If this is user generated input, you may want to add more input validation.

Sign up to request clarification or add additional context in comments.

Comments

1

just implemented your code - works perfect, but did some modifications, maybe it could help somehow:

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
    $dec_arr = array();
    foreach($binary_ip_arr as $value) { #convert each octet to decimal
        $dec_arr[] = bindec($value);
    }
    $ddn = implode('.',$dec_arr); #convert to string
    return $ddn;
}

echo bin_to_ddn('11000111000000010000000101100000');

echo bin_to_ddn('11000111000000010000000101111111');

2 Comments

Hey thanks. What kind of improvement would this bring over my code?
when you use some tricks like &$value and $value = bindec($value); it is not very clear for me. I am trying to avoid if possible this &$ so finally you had some spaces in your string?
0

There are built-in functions to do these operations:

echo long2ip(bindec('11000111000000010000000101111111'));

Or in the function:

function bin_to_ddn($binary_ip) #converts a binary IP in DDN
{
    return long2ip(bindec((string)$binary_ip));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.