Formats bytes into a human readable string.

@param int $bytes

@return string
543587
public static function formatBytes($bytes)
    {
        $bytes = (int) $bytes;

        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');

        return round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 3) . ' ' . $units[$i];
    }
735157
public static function Bytes($Bytes, $Precision = 2) {
      $Units = array('B', 'K', 'M', 'G', 'T');
      $Bytes = max($Bytes, 0);
      $Pow = floor(($Bytes ? log($Bytes) : 0) / log(1024));
      $Pow = min($Pow, count($Units) - 1);
      $Bytes /= pow(1024, $Pow);
      return round($Bytes, $Precision) . $Units[$Pow]; 
   }
596066
public function EntryRead($Bytes = -1)
    {
        $Actual = $this->EntryActualSize();
        if ($Bytes < 0 || $Bytes > $Actual) {
            $Bytes = $Actual;
        }
        return substr($this->Data, $this->Offset + 512, $Bytes);
    }
1147723
protected function formatBytes($bytes)
    {
        $bytes = (int) $bytes;

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

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

        return $bytes . ' B';
    }
119143
public function toBytes(): string
    {
        $bytes = '';
        $hex = $this->toHex();

        foreach (range(0, 30, 2) as $i) {
            $bytes .= chr(hexdec(substr($hex, $i, 2)));
        }

        return $bytes;
    }
943407
public static function formatBytes($bytes, $precision = 2)
    {
        if ($bytes < 1024) {
            return $bytes.' B';
        } elseif ($bytes < 1048576) {
            return round($bytes / 1024, $precision).' KB';
        } elseif ($bytes < 1073741824) {
            return round($bytes / 1048576, $precision).' MB';
        } elseif ($bytes < 1099511627776) {
            return round($bytes / 1073741824, $precision).' GB';
        } else {
            return round($bytes / 1099511627776, $precision).' TB';
        }
    }
384803
public static function convertSizeToText(string $bytes): string
    {
        $kb = 1 << 10;
        $mb = $kb << 10;
        $gb = $mb << 10;
        $tb = $gb << 10;

        if ($bytes < $kb) {
            return $bytes . ' Б';
        } elseif ($bytes < $mb) {
            return number_format($bytes / $kb, 2, ',', '') . ' КБ';
        } elseif ($bytes < $gb) {
            return number_format($bytes / $mb, 2, ',', '') . ' MБ';
        } elseif ($bytes < $tb) {
            return number_format($bytes / $gb, 2, ',', '') . ' ГБ';
        }

        return number_format($bytes / $tb, 2, ',', '') . ' ТБ';
    }
1281164
private function toDecimal(string $bytes) : string
    {
        $result = '0';
        $power = '1';

        for ($i = \strlen($bytes) - 1; $i >= 0; $i--) {
            $index = \ord($bytes[$i]);

            if ($index !== 0) {
                $result = $this->add($result, ($index === 1)
                    ? $power
                    : $this->mul($power, (string) $index)
                );
            }

            if ($i !== 0) {
                $power = $this->mul($power, '256');
            }
        }

        return $result;
    }
204417
private function formatBytes($bytes)
    {
        if ($bytes === 0) {
            return 0;
        }

        $units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
        $base  = log($bytes, 1024);
        $floor = floor($base);
        $unit  = $units[$floor];
        $size  = round(pow(1024, ($base - $floor)), 2);

        $locale = localeconv();
        $size   = number_format($size, 2, $locale['decimal_point'], $locale['thousands_sep']);

        return rtrim($size, '.0').' '.$unit;
    }
1350125
public static function format($bytes, $decimals = 2): string
    {
        $exp = 0;
        $value = 0;
        $symbol = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

        $bytes = (float)$bytes;

        if ($bytes > 0) {
            $exp = floor(log($bytes) / log(1024));
            $value = ($bytes / (1024 ** floor($exp)));
        }

        if ($symbol[$exp] === 'B') {
            $decimals = 0;
        }

        return number_format($value, $decimals, '.', '') . ' ' . $symbol[$exp];
    }
1148960
public function byteIntToString($int)
    {
        if ($int % 1073741824 == 0) { // 1024 * 1024 * 1024
            return ($int / 1073741824).'G';
        } elseif ($int % 1048576 == 0) { // 1024 * 1024
            return ($int / 1048576).'M';
        } elseif ($int % 1024 == 0) {
            return ($int / 1024).'K';
        }

        return (string) $int;
    }
1333533
public function write(string $bytes): int
    {
        return $this->outputStream->write(iconv('UTF-8', $this->charset, $bytes));
    }
663707
private static function sizeFormat($bytes)
    {
        $kb = 1024;
        $mb = $kb * 1024;
        $gb = $mb * 1024;
        $tb = $gb * 1024;

        if (($bytes >= 0) && ($bytes < $kb)) {
            return $bytes . ' B';
        } elseif (($bytes >= $kb) && ($bytes < $mb)) {
            return ceil($bytes / $kb) . ' KB';
        } elseif (($bytes >= $mb) && ($bytes < $gb)) {
            return ceil($bytes / $mb) . ' MB';
        } elseif (($bytes >= $gb) && ($bytes < $tb)) {
            return ceil($bytes / $gb) . ' GB';
        } elseif ($bytes >= $tb) {
            return ceil($bytes / $tb) . ' TB';
        } else {
            return $bytes . ' B';
        }
    }
216932
public function formatFileSize ($bytes)
    {
        $sz = 'BKMGTP';
        $factor = (int) floor((strlen($bytes) - 1) / 3);
        return number_format($bytes / pow(1024, $factor), 2, ",", ".") . @$sz[$factor];
    }
57516
public function show_bytes($bytes, &$unit = null)
    {
        if ($bytes >= 1073741824) {
            $unit = 'GB';
            $gb   = $bytes/1073741824;
            $str  = sprintf($gb >= 10 ? "%d " : "%.1f ", $gb) . $this->gettext($unit);
        }
        else if ($bytes >= 1048576) {
            $unit = 'MB';
            $mb   = $bytes/1048576;
            $str  = sprintf($mb >= 10 ? "%d " : "%.1f ", $mb) . $this->gettext($unit);
        }
        else if ($bytes >= 1024) {
            $unit = 'KB';
            $str  = sprintf("%d ",  round($bytes/1024)) . $this->gettext($unit);
        }
        else {
            $unit = 'B';
            $str  = sprintf('%d ', $bytes) . $this->gettext($unit);
        }

        return $str;
    }
352376
public static function fromBytes(array $bytes)
    {
        $result = null;
        if (count($bytes) === 4) {
            $chunks = array_map(
                function ($byte) {
                    return (is_int($byte) && $byte >= 0 && $byte <= 255) ? (string) $byte : false;
                },
                $bytes
            );
            if (in_array(false, $chunks, true) === false) {
                $result = new static(implode('.', $chunks));
            }
        }

        return $result;
    }
352473
public function read($bytes)
    {
        $read = 0;
        $parts = array();

        while ($read < $bytes) {
            $data = fread($this->socket, $bytes - $read);

            if ($data === false) {
                // server must have dropped the connection
                if (feof($this->socket)) {
                    $this->close();
                }

                return false;
            }

            $read += strlen($data);
            $parts[] = $data;
        }

        return implode($parts);
    }