Return unpack ('C *', "string")

I would like to know how I can cancel this unpack function. I think the pack function is able to undo what unpack is , but I'm not sure.

First I have a simple string, which after unpacking I will have an array of bytes representing such a string. Now I would like to know how to cancel such an array back to the original string.

<?php $array = unpack('C*', "odd string"); /*Output: Array ( [1] => 111 [2] => 100 [3] => 100 [4] => 32 [5] => 115 [6] => 116 [7] => 114 [8] => 105 [9] => 110 [10] => 103 )*/ $string = pack("which format here?", $array); echo $string; #Desired Output: odd string ?> 

Thanks.

+4
source share
2 answers

You should use call_user_func_array to return unpack('C*', "string") , for example:

 call_user_func_array('pack', array_merge(array('C*'), $array ))) 
+10
source

When you are on PHP 5.6, you should use the decompress argument , as it is about 4-5 times faster than using call_user_func_array

 pack('C*', ...$array); 

And when you are on PHP 5.5 or lower, you should use ReflectionFunction , which seems to be a little faster than call_user_func_array :

 $packFunction = new ReflectionFunction('pack'); $packFunction->invokeArgs(array_merge(array('C*'), $array)); 
+3
source

Source: https://habr.com/ru/post/1437316/


All Articles