Unsigned int to signed in php

后端 未结 4 1154
别跟我提以往
别跟我提以往 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:56

    -Misunderstood problem, see Paul Dixon's answer above.


    64bit unsigned integers are not technically supported in PHP5. It will use the native type. To convert to a 32bit signed int from a 64bit signed int without losing the high bit, you could mask and then type cast the output:

    $ip_int = ip2long($ip);
    if (PHP_INT_SIZE == 8) // 64bit native
    {
      $temp_int = (int)(0x7FFFFFFF & $ip_int);
      $temp_int |= (int)(0x80000000 & ($ip_int >> 32));
      $ip_int = $temp_int;
    }
    

    On a 64 bit system, printing this value ($ip_int) will display an 'unsigned' integer since we've removed the high bit. However this should allow you to take the output and store it how you wish.

提交回复
热议问题