lib/danog/PHP/StructTools.php
public function pack($format, ...$data)
    {
        $format = $this->padformat($format);
        $result = null; // Data to return
        $size = $this->calcsize($format);
        $packcommand = $this->parseformat($format, $this->array_each_strlen($data)); // Get pack parameters
        set_error_handler([$this, 'ExceptionErrorHandler']);
        foreach ($packcommand as $key => $command) {
            try {
                switch ($command['modifiers']['TYPE']) {
                    case 'int':
                        if (!is_int($data[$command['datakey']]) && !is_float($data[$command['datakey']])) {
                            $data[$command['datakey']] = (int) $data[$command['datakey']];
                        }
                        break;
                    case 'float':
                        if (!is_float($data[$command['datakey']])) {
                            $data[$command['datakey']] = (float) $data[$command['datakey']];
                        }
                        break;
                    case 'string':
                        if (!is_string($data[$command['datakey']])) {
                            $data[$command['datakey']] = (string) $data[$command['datakey']];
                        }
                        break;
                    case 'bool':
                        if (!is_bool($data[$command['datakey']])) {
                            $data[$command['datakey']] = (bool) $data[$command['datakey']];
                        }
                        break;
                    default:
                        break;
                }
                switch ($command['phpformat']) {
                    case 'x':
                        $curresult = pack($command['phpformat'].$command['count']); // Pack current char
                        break;
                    case 'p':
                        $curresult = pack('c', ($command['count'] - 1 > 255) ? 255 : $command['count'] - 1).pack('a'.($command['count'] - 1), $data[$command['datakey']]);
                        break;

                    case 'q':
                    case 'Q':
                    case 'l':
                    case 'L':
                    case 'i':
                    case 'I':
                    case 's':
                    case 'S':
                    case 'c':
                    case 'C':
                        $curresult = $this->num_pack($data[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));

                        break;

                    case '?':
                        $curresult = pack('c'.$command['count'], $data[$command['datakey']]); // Pack current char
                        break;
                    default:
                        $curresult = pack($command['phpformat'].$command['count'], $data[$command['datakey']]); // Pack current char
                        break;
                }
                if (strlen($curresult) != $command['modifiers']['SIZE'] * $command['count']) {
                    trigger_error('Size of packed data '.strlen($curresult)." isn't equal to expected size ".$command['modifiers']['SIZE'] * $command['count'].'.');
                }
            } catch (StructException $e) {
                throw new StructException('An error occurred while packing '.$data[$command['datakey']].' at format key '.$command['format'].' ('.$e->getMessage().').');
            }
            if ($command['modifiers']['FORMAT_ENDIANNESS'] != $command['modifiers']['BIG_ENDIAN']) {
                $curresult = strrev($curresult);
            } // Reverse if wrong endianness
            /*
            if (strlen($curresult) > $command['modifiers']['SIZE'] * $command['count']) {
                if ($command['modifiers']['BIG_ENDIAN']) {
                    $curresult = strrev($curresult);
                }
                $remains = array_slice(str_split($curresult), $command['modifiers']['SIZE'], strlen($curresult) - $command['modifiers']['SIZE']);
                foreach ($remains as $rem) {
                    if ($rem != '') {
                        throw new StructException('Error while trimming result at offset '.$key.' (format char '.$command['format']."): data to trim isn't empty.");
                    }
                }
                $curresult = implode('', substr($curresult, 0, $command['modifiers']['SIZE']));
                if ($command['modifiers']['BIG_ENDIAN']) {
                    $curresult = strrev($curresult);
                }
            }*/
            $result .= $curresult;
        }
        restore_error_handler();
        if (strlen($result) != $size) {
            throw new StructException('Length of generated data ('.strlen($result).') is different from length calculated using format string ('.$size.').');
        }

        return $result;
    }
public function pack($format, ...$data)
    {
        $format = $this->padformat($format);
        $result = null; // Data to return
        $size = $this->calcsize($format);
        $packcommand = $this->parseformat($format, $this->array_each_strlen($data)); // Get pack parameters
        set_error_handler([$this, 'ExceptionErrorHandler']);
        foreach ($packcommand as $key => $command) {
            try {
                switch ($command['modifiers']['TYPE']) {
                    case 'int':
                        if (!is_int($data[$command['datakey']]) && !is_float($data[$command['datakey']])) {
                            $data[$command['datakey']] = (int) $data[$command['datakey']];
                        }
                        break;
                    case 'float':
                        if (!is_float($data[$command['datakey']])) {
                            $data[$command['datakey']] = (float) $data[$command['datakey']];
                        }
                        break;
                    case 'string':
                        if (!is_string($data[$command['datakey']])) {
                            $data[$command['datakey']] = (string) $data[$command['datakey']];
                        }
                        break;
                    case 'bool':
                        if (!is_bool($data[$command['datakey']])) {
                            $data[$command['datakey']] = (bool) $data[$command['datakey']];
                        }
                        break;
                    default:
                        break;
                }
                switch ($command['phpformat']) {
                    case 'x':
                        $curresult = pack($command['phpformat'].$command['count']); // Pack current char
                        break;
                    case 'p':
                        $curresult = pack('c', ($command['count'] - 1 > 255) ? 255 : $command['count'] - 1).pack('a'.($command['count'] - 1), $data[$command['datakey']]);
                        break;

                    case 'q':
                    case 'Q':
                    case 'l':
                    case 'L':
                    case 'i':
                    case 'I':
                    case 's':
                    case 'S':
                    case 'c':
                    case 'C':
                        $curresult = $this->num_pack($data[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));

                        break;

                    case '?':
                        $curresult = pack('c'.$command['count'], $data[$command['datakey']]); // Pack current char
                        break;
                    default:
                        $curresult = pack($command['phpformat'].$command['count'], $data[$command['datakey']]); // Pack current char
                        break;
                }
                if (strlen($curresult) != $command['modifiers']['SIZE'] * $command['count']) {
                    trigger_error('Size of packed data '.strlen($curresult)." isn't equal to expected size ".$command['modifiers']['SIZE'] * $command['count'].'.');
                }
            } catch (StructException $e) {
                throw new StructException('An error occurred while packing '.$data[$command['datakey']].' at format key '.$command['format'].' ('.$e->getMessage().').');
            }
            if ($command['modifiers']['FORMAT_ENDIANNESS'] != $command['modifiers']['BIG_ENDIAN']) {
                $curresult = strrev($curresult);
            } // Reverse if wrong endianness
            /*
            if (strlen($curresult) > $command['modifiers']['SIZE'] * $command['count']) {
                if ($command['modifiers']['BIG_ENDIAN']) {
                    $curresult = strrev($curresult);
                }
                $remains = array_slice(str_split($curresult), $command['modifiers']['SIZE'], strlen($curresult) - $command['modifiers']['SIZE']);
                foreach ($remains as $rem) {
                    if ($rem != '') {
                        throw new StructException('Error while trimming result at offset '.$key.' (format char '.$command['format']."): data to trim isn't empty.");
                    }
                }
                $curresult = implode('', substr($curresult, 0, $command['modifiers']['SIZE']));
                if ($command['modifiers']['BIG_ENDIAN']) {
                    $curresult = strrev($curresult);
                }
            }*/
            $result .= $curresult;
        }
        restore_error_handler();
        if (strlen($result) != $size) {
            throw new StructException('Length of generated data ('.strlen($result).') is different from length calculated using format string ('.$size.').');
        }

        return $result;
    }
pack.

Packs data into bytes

@param	$format		Format string
@param	...$data	Parameters to encode

@return Encoded data