phpbinarydecimal32-bit

Binary to Decimal 32bit signed with PHP


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.


Solution

  • 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)