Search code examples
phppackunpack

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


I would like to know how I can reverse what this unpack function bellow performed. I think the pack function is able to reverse what unpack performed, however I'm not sure.

First I have a simple string which after unpacking it I would have an array of bytes representing such string. Now I would like to know how to reverse such 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
?>

Thank you.


Solution

  • You should use call_user_func_array to revert unpack('C*', “string”), like this:

    call_user_func_array('pack', array_merge(array('C*'), $array )))