Home > OS >  Binary to Decimal 32bit signed with PHP
Binary to Decimal 32bit signed with PHP

Time:03-17

I currently have a Decimal that is coming through as: 4294960896

This is then converted to Binary with this function:

decbin('4294960896')

Which equals: 11111111111111111110011100000000

I then need to take that binary and convert it to the 32bit signed version which should be '-6400'

I can't seem to find any built in functions that support 32bit signed output.

CodePudding user response:

You can use bindec() function. try below code. You can check official documentation as well.

$value = '11111111111111111110011100000000';
echo bindec($value);  //4294960896

CodePudding user response:

The hexadecimal representation of the number 4294960896 is FFFFE700h. This should be interpreted as 32 bit signed long. The functions that support such tasks are pack and unpack.

$i64 = 4294960896;

$i32 = unpack('l',pack('V',$i64))[1];
//int(-6400)
  • Related