8

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 convert integer to byte array in php):

<?php

$i = 123456;
$ar = unpack("C*", pack("L", $i));

print_r($ar);
?>

The above code will output:

//output:
Array
(
   [1] => 64
   [2] => 226
   [3] => 1
   [4] => 0
)

But my problem right now is how to reverse this process. Meaning converting from byte array into integer. In the case above, the output will be 123456

Can anybody help me with this. I would be a great help. Thanks ahead.

3
  • try to type casting for this. Commented Oct 2, 2012 at 2:39
  • Possible duplicate of Reverting unpack('C*', "string") Commented Dec 25, 2017 at 2:05
  • Here is how to covert byte array into float in PHP. Commented Jun 27, 2019 at 8:13

3 Answers 3

11

Why not treat it like the math problem it is?

$i = ($ar[3]<<24) + ($ar[2]<<16) + ($ar[1]<<8) + $ar[0];
Sign up to request clarification or add additional context in comments.

2 Comments

wow, this solve my problem but I change the code into: $i = ($ar[4]<<24) + ($ar[3]<<16) + ($ar[2]<<8) + ($ar[1]); since $ar[0] doesn't exist and when no parentheses is added between plus sign it will return 0 value. but thanks.
@gchimuel The parentheses around last part $ar[0] is unnecessary, other parts is.
5

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]));

3 Comments

You can also do unpack('L', pack('C*', ...array_reverse($ar))) if you can ensure that $ar have 4 items and being indexed from 0 to 3.
Also note the output of unpack('C*', $someBytesArray) will starts index from 1, so you should wrap array_values() on it.
The meaning of array_reverse() or writing literal $ar[3], ..., $ar[0] is to convert byte order between big and small endian, if you can ensure the same endian is used through pack() to unpack(), you don't have to reverse them.
4

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;

1 Comment

Worth to mention that this workaround is needed for x64 builds of PHP (except Windows ones). And there is one mistake in the code. The comparison must be greater or equal instead of just greater.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.