Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast signed to unsigned int for 32-bit in php

Tags:

php

I have tried to cast signed int to unsigned int for 32-bit in php. but i could not able to get the absolute value for the unsigned int.

example:

 $value = 65547;
  if (PHP_INT_SIZE == 8)
   {
    if ($value>0x7FFFFFFF)
    {
      $value-=0x100000000;
    }
   }

Is any other possibilities to cast signed integer value to unsigned integer value?

like image 983
Nandhini Dheenan Avatar asked Oct 23 '25 16:10

Nandhini Dheenan


1 Answers

http://php.net/manual/en/function.gmp-abs.php

For php7 you will need the php7.0-gmp package

For php5 you will need the php5-gmp package

var_dump((int) gmp_abs("0x7FFFFFFF"));

... returns

int(2147483647)
like image 98
Jamie_D Avatar answered Oct 26 '25 05:10

Jamie_D