„pack” a value (i.e. write the binary representation as big endian to a string) with the specified size
@param integer $nbytes The number of bytes to pack
@param integer $int The integer value to pack
@return string The packed value as string
358241
public static function packItWithSize($nbytes, $int)
    {
        $formats = array("C", "n", "N", "N");
        $format = $formats[$nbytes-1];

        if ($nbytes == 3) {
            return substr(pack($format, $int), -3);
        }
        return pack($format, $int);
    }
1142273
public static function bit64(int $data, $endianness = false): string
    {
        // big-endian
        if (true === $endianness) {
            return pack('J', $data);
        }

        // little-endian
        if (false === $endianness) {
            return pack('P', $data);
        }

        // machine byte order
        if (null === $endianness) {
            return pack('Q', $data);
        }
    }
7651
@Override
    public int encode ( byte[] dst, int dstIndex ) {
        int start = dstIndex;
        SMBUtil.writeInt8(this.allocationSize, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeInt8(this.endOfFile, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeInt4(this.numberOfLinks, dst, dstIndex);
        dstIndex += 4;
        dst[ dstIndex++ ] = (byte) ( this.deletePending ? 1 : 0 );
        dst[ dstIndex++ ] = (byte) ( this.directory ? 1 : 0 );
        return dstIndex - start;
    }
133739
public static function formatBytes($bytes)
    {
        $bytes = (double) abs($bytes);
        if ($bytes === 0.0) {
            return '0.00 B';
        }
        //units
        $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
        $index = (int) (log($bytes)/log(1024));
        //echo $index;
        if ($index > 8) {
            throw new \OutOfRangeException('Input is to large! Maximum supported: 1.236731113465765645724418048 * 10^27 bytes.');
        }
        if ($index === 0) {
            //format
            $output = sprintf('%.2f %s', $bytes, $units[$index]);
        } else {
            //format
            $output = sprintf('%.2f %s', ($bytes/pow(1024, $index)), $units[$index]);
        }

        return $output;
    }
100955
protected function formatBytes(int $bytes)
    {
        if (!$this->useFormatting) {
            return $bytes;
        }

        if ($bytes > 1024 * 1024) {
            return round($bytes / 1024 / 1024, 2).' MB';
        } elseif ($bytes > 1024) {
            return round($bytes / 1024, 2).' KB';
        }

        return $bytes . ' B';
    }
933918
public static function byteStringToInt($bytes)
    {
        if (!self::isSupported()) {
            throw new \RuntimeException('This utility is only supported on 64 bit machines with PHP version > 5.5.');
        }
        if (self::isSystemLittleEndian()) {
            $bytes = strrev($bytes);
        }
        return unpack("q", $bytes)[1];
    }
1317545
public function unpack($packed)
    {
        if (!$packed instanceof Binary) {
            throw new InvalidArgumentException("packed value should be BSON binary");
        }

        return unserialize((string)$packed, $this->options);
    }
96576
function binaryToLong($str)
    {
        if ($str === null) {
            return null;
        }

        // Use array_merge to return a zero-indexed array instead of a
        // one-indexed array.
        $bytes = array_merge(unpack('C*', $str));

        $n = $this->init(0);

        if ($bytes && ($bytes[0] > 127)) {
            trigger_error("bytesToNum works only for positive integers.",
                          E_USER_WARNING);
            return null;
        }

        foreach ($bytes as $byte) {
            $n = $this->mul($n, pow(2, 8));
            $n = $this->add($n, $byte);
        }

        return $n;
    }
683937
private static byte[] writeInt(int value) throws IOException {
        byte[] b = new byte[4];

        b[0] = (byte) (value & 0x000000FF);
        b[1] = (byte) ((value & 0x0000FF00) >> 8);
        b[2] = (byte) ((value & 0x00FF0000) >> 16);
        b[3] = (byte) ((value & 0xFF000000) >> 24);

        return b;
    }
113323
@Override
    public int encode ( byte[] dst, int dstIndex ) {
        int start = dstIndex;
        SMBUtil.writeTime(this.createTime, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeTime(this.lastAccessTime, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeTime(this.lastWriteTime, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeTime(this.changeTime, dst, dstIndex);
        dstIndex += 8;
        SMBUtil.writeInt4(this.attributes, dst, dstIndex);
        dstIndex += 4;
        dstIndex += 4;
        return dstIndex - start;
    }
958193
function formatBytes($bytes)
    {
        $bytes = floatval($bytes);
        $arBytes = array(
            0 => array(
                "UNIT" => "TB",
                "VALUE" => pow(1024, 4)
            ),
            1 => array(
                "UNIT" => "GB",
                "VALUE" => pow(1024, 3)
            ),
            2 => array(
                "UNIT" => "MB",
                "VALUE" => pow(1024, 2)
            ),
            3 => array(
                "UNIT" => "KB",
                "VALUE" => 1024
            ),
            4 => array(
                "UNIT" => "B",
                "VALUE" => 1
            ),
        );

        foreach($arBytes as $arItem)
        {
            if($bytes >= $arItem["VALUE"])
            {
                $result = $bytes / $arItem["VALUE"];
                $result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];
                break;
            }
        }
        return $result;
    }
1277759
static function convertToBytes($from) {
		if (is_numeric($from)) {
			return (int)$from;
		}

		$number = substr($from, 0, -2);
		$formats = ["KB", "MB", "GB", "TB"];
		$format = strtoupper(substr($from, -2));

		return in_array($format, $formats) ? (int)($number * pow(1024, array_search($format, $formats) + 1)) : (int)$from;
	}
120089
private function toZigZag($number, $bits)
    {
        if (PHP_INT_SIZE === 4) {
            $number = gmp_init($number, 10);
        }
        $result = ($number << 1) ^ ($number >> ($bits - 1));
        if (PHP_INT_SIZE === 4) {
            $result = gmp_strval($result, 10);
        }

        return $result;
    }
1127443
protected function pack($data, int $seconds = 0): string
    {
        return serialize(
            array(
                'data' => $this->toPack($data),
                'timeout' => $seconds ? $this->microtime($seconds * 1000) : 0,
            )
        );
    }
1373142
public static function int2bytes($len, $int = 0, $l = false)
    {
        $hexstr = dechex($int);

        if ($len === null) {
            if (strlen($hexstr) % 2) {
                $hexstr = "0" . $hexstr;
            }
        } else {
            $hexstr = str_repeat('0', $len * 2 - strlen($hexstr)) . $hexstr;
        }

        $bytes = strlen($hexstr) / 2;
        $bin   = '';

        for ($i = 0; $i < $bytes; ++$i)
        {
            $bin .= chr(hexdec(substr($hexstr, $i * 2, 2)));
        }

        return $l ? strrev($bin) : $bin;
    }
1123072
public static function bit64(string $data, int $offset = 0, bool $endianness = false): int
    {
        // big-endian
        if (true === $endianness) {
            return unpack('J', $data, $offset)[1];
        }

        // little-endian
        if (false === $endianness) {
            return unpack('P', $data, $offset)[1];
        }

        // machine byte order
        if (null === $endianness) {
            return unpack('Q', $data, $offset)[1];
        }
    }
756259
private int getLengthFromByteArray(byte[] bin){
        ByteBuffer bb = ByteBuffer.wrap(bin);
        bb.order(ByteOrder.BIG_ENDIAN);
        return bb.getShort();
    }