Java Byte Array to String bytesToString(long bytes)

Here you can find the source of bytesToString(long bytes)

Description

Returns a human-readable representation of a given number of bytes.

License

Open Source License

Parameter

Parameter Description
bytes the number of bytes

Return

the string representation

Declaration

public static String bytesToString(long bytes) 

Method Source Code

//package com.java2s;
/*//from w ww  .  ja  va  2s . co m
 * opsu! - an open-source osu! client
 * Copyright (C) 2014, 2015 Jeffrey Han
 *
 * opsu! is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opsu! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opsu!.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns a human-readable representation of a given number of bytes.
     * @param bytes the number of bytes
     * @return the string representation
     * @author aioobe (http://stackoverflow.com/a/3758880)
     */
    public static String bytesToString(long bytes) {
        if (bytes < 1024)
            return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(1024));
        char pre = "KMGTPE".charAt(exp - 1);
        return String.format("%.1f %cB", bytes / Math.pow(1024, exp), pre);
    }
}

Related

  1. bytesToString(final byte[] data)
  2. bytesToString(int bytes)
  3. bytesToString(int bytes)
  4. bytesToString(int... bytesInt)
  5. bytesToString(long b)
  6. bytesToString(long bytes)
  7. bytesToString(long size)
  8. byteTo16String(byte[] bt)
  9. byteToMacString(byte[] data)