Page 1 of 1

Calculating the difference between two IPv6 addresses.

Posted: Fri Feb 20, 2009 8:29 am
by kaisellgren
Hi,

I am trying to calculate the difference between two IPv6 addresses.

Here is what I have done so far:

Code: Select all

function diff($ip,$ip2)
 {
  $parts = explode(':',$ip);
  $value = 0;
  for ($a = 7;$a > 0;$a--)
   $value += hexdec($parts[$a]) * (pow(0xFFFF,7-$a));
  $parts = explode(':',$ip);
  $value2 = 0;
  for ($a = 7;$a > 0;$a--)
   $value2 += hexdec($parts[$a]) * (pow(0xFFFF,7-$a));
  return (abs($value-$value2));
 }
 
$ip = '2001:0db8:0000:0000:0000:0000:1420:57ab';
$ip2 = '2001:0db8:0000:0000:0000:0000:1420:57ac';
echo diff($ip,$ip2);
These two IPv6 addresses are same except for the last character - so the difference should be 1 in this case, but the script outputs 0. I think this is a rounding issue?