How to convert byte array to integer in php?

前端 未结 3 1932
难免孤独
难免孤独 2020-12-11 05:10

I have seen a code that converts integer into byte array. Below is the code on How to convert integer to byte array in php 3 (How to conver

相关标签:
3条回答
  • 2020-12-11 06:10

    In order to get a signed 4-byte value in PHP you need to do this:

    $temp = ($ar[0]<<24) + ($ar[1]<<16) + ($ar[2]<<8) + ($ar[3]);
    if($temp > 2147483648)
         $temp -= 4294967296;
    
    0 讨论(0)
  • 2020-12-11 06:14

    Why not treat it like the math problem it is?

    $i = ($ar[3]<<24) + ($ar[2]<<16) + ($ar[1]<<8) + $ar[0];

    0 讨论(0)
  • 2020-12-11 06:17

    Since L is four bytes long, you know the number of elements of the array. Therefore you can simply perform the operation is reverse:

    $ar = [64,226,1,0];
    $i = unpack("L",pack("C*",$ar[3],$ar[2],$ar[1],$ar[0]));
    
    0 讨论(0)
提交回复
热议问题