lib/danog/PHP/StructTools.php
public function num_pack($n, $blocksize, $unsigned)
    {
        $bitnumber = $blocksize * 8;
        if ($unsigned) {
            $min = 0;
            switch ($bitnumber) {
                case '8':
                    $max = 255;
                    break;
                case '16':
                    $max = 65535;
                    break;
                case '32':
                    $max = 4294967295;
                    break;
                case '64':
                    $max = 18446744073709551615;
                    break;
                default:
                    $max = pow(2, $bitnumber) - 1;
                    break;
            }
        } else {
            switch ($bitnumber) {
                case '8':
                    $min = -128;
                    $max = 127;
                    break;
                case '16':
                    $min = -32768;
                    $max = 32767;
                    break;
                case '32':
                    $min = -2147483648;
                    $max = 2147483647;
                    break;
                case '64':
                    $min = -9223372036854775808;
                    $max = 9223372036854775807;
                    break;
                default:
                    $max = pow(2, $bitnumber - 1) - 1;
                    $min = -pow(2, $bitnumber - 1);
                    break;
            }
        }
        if ($n < $min || $n > $max) {
            trigger_error('Number is not within required range ('.$min.' <= number <= '.$max.').');
        }
        $bits = $this->decbin($n, $bitnumber);
        $s = '';
        foreach (str_split($bits, 8) as $byte) {
            $s .= chr($this->bindec($byte));
        }

        if (strlen($s) > $blocksize) {
            trigger_error('Generated data length ('.strlen($s).') is bigger than required length ('.$blocksize.').');
        }

        return $s;
    }
public function num_pack($n, $blocksize, $unsigned)
    {
        $bitnumber = $blocksize * 8;
        if ($unsigned) {
            $min = 0;
            switch ($bitnumber) {
                case '8':
                    $max = 255;
                    break;
                case '16':
                    $max = 65535;
                    break;
                case '32':
                    $max = 4294967295;
                    break;
                case '64':
                    $max = 18446744073709551615;
                    break;
                default:
                    $max = pow(2, $bitnumber) - 1;
                    break;
            }
        } else {
            switch ($bitnumber) {
                case '8':
                    $min = -128;
                    $max = 127;
                    break;
                case '16':
                    $min = -32768;
                    $max = 32767;
                    break;
                case '32':
                    $min = -2147483648;
                    $max = 2147483647;
                    break;
                case '64':
                    $min = -9223372036854775808;
                    $max = 9223372036854775807;
                    break;
                default:
                    $max = pow(2, $bitnumber - 1) - 1;
                    $min = -pow(2, $bitnumber - 1);
                    break;
            }
        }
        if ($n < $min || $n > $max) {
            trigger_error('Number is not within required range ('.$min.' <= number <= '.$max.').');
        }
        $bits = $this->decbin($n, $bitnumber);
        $s = '';
        foreach (str_split($bits, 8) as $byte) {
            $s .= chr($this->bindec($byte));
        }

        if (strlen($s) > $blocksize) {
            trigger_error('Generated data length ('.strlen($s).') is bigger than required length ('.$blocksize.').');
        }

        return $s;
    }
num_pack.

Convert a number to a big endian byte string.
If optional blocksize is given and greater than zero, pad the front of the
byte string with binary zeros so that the length is the
blocksize.

@param	$n		    Number to pack
@param	$blocksize	Block size
@param   $unsigned Boolean that determines whether to work in signed or unsigned mode

@return Byte string