Jump to content

ip range convert bug after 2147483647


Oziam

Recommended Posts

I have a simple script that converts an ip range to a readable ip address,

 

e.g 2147483647 = 127.255.255.255 and it works fine up to this range,

anything after this causes an incorrect ip address.

 

e.g 2147483648 should be 128.0.0.0 but it outputs 128.0.0.255 and so all ip address after this are incorrect!

 

code I am using is below;

 

$rawip_from = '2147483648';

// raw to readable ip
$w1 = (int)($rawip_from / 16777216) % 256;
$x1 = (int)($rawip_from / 65536   ) % 256;
$y1 = (int)($rawip_from / 256     ) % 256;
$z1 = (int)($rawip_from           ) % 256;

$rawip_to = '2147549184';
// raw to readable ip
$w2 = (int)($rawip_to / 16777216) % 256;
$x2 = (int)($rawip_to / 65536   ) % 256;
$y2 = (int)($rawip_to / 256     ) % 256;
$z2 = (int)($rawip_to           ) % 256;

$ip_from = ($w1.'.'.$x1.'.'.$y1.'.'.$z1);
$ip_to = ($w2.'.'.$x2.'.'.$y2.'.'.$z2);

echo "Ip raw range: $rawip_from - $rawip_to<p>Ip range: $ip_from - $ip_to";

/* should output 
Ip raw range: 2147483648 - 2164260863

Ip range: 128.0.0.0 - 128.1.0.0

but it outputs 128.0.0.255 - 128.1.0.255
*/

It always seems to add 255 to the last range instead of the correct value?????

I heard there is a bug in php with integers > 2147483647

 

if anyone knows of a fix it would be great!!

 

 

Thanks...

Link to comment
Share on other sites

I worked out a solution, I was playing around and stumbled across it,

I don't know why it works but it does 100% of the time, I have tested it using

all variations of ranges and it outputs the correct value every time!

 

New code!

 

$rawip_from = '3716537112'-0; // adding the -0 or +0 will return a negative value of 221.133.219.-232

// raw to readable ip
$w1 = (int)($rawip_from / 16777216) % 256;
$x1 = (int)($rawip_from / 65536   ) % 256;
$y1 = (int)($rawip_from / 256     ) % 256;
$z1 = (int)($rawip_from           ) % 256;

if($z1 < 0){$z1 = $z1 + 256;} // include this to convert $z1 to the correct value

$rawip_to = '3716537215'-0; // this will return a negative value of 221.133.219.-129
// raw to readable ip
$w2 = (int)($rawip_to / 16777216) % 256;
$x2 = (int)($rawip_to / 65536   ) % 256;
$y2 = (int)($rawip_to / 256     ) % 256;
$z2 = (int)($rawip_to           ) % 256;

if($z2 < 0){$z2 = $z2 + 256;} // include this to convert $z2 to the correct value

$ip_from = ($w1.'.'.$x1.'.'.$y1.'.'.$z1);
$ip_to = ($w2.'.'.$x2.'.'.$y2.'.'.$z2);

echo "Ip range: $ip_from - $ip_to";

// Output: Ip range: 221.133.219.24 - 221.133.219.127 which is correct :-)

 

It would be nice to understand why it works but hey I am just greatful it does!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.