Unsigned int to signed in php

后端 未结 4 1153
别跟我提以往
别跟我提以往 2021-01-06 00:43

It appears, that in 32bit OS ip2long returns signed int, and in 64bit OS unsigned int is returned.

My application is working on 10 servers, and some are

4条回答
  •  青春惊慌失措
    2021-01-06 00:46

    PHP does not support unsigned integers as a type, but what you can do is simply turn the result of ip2long into an unsigned int string by having sprintf interpret the value as unsigned with %u:

     $ip="128.1.2.3";
     $signed=ip2long($ip);             // -2147417597 in this example
     $unsigned=sprintf("%u", $signed); //  2147549699 in this example
    

    Edit, since you really wanted it to be signed even on 64 bit systems - here's how you'd convert the 64 bit +ve value to a 32 bit signed equivalent:

    $ip = ip2long($ip);
    if (PHP_INT_SIZE == 8)
    {
        if ($ip>0x7FFFFFFF)
        {
            $ip-=0x100000000;
        }
    }
    

提交回复
热议问题