Java Utililty Methods Byte Array to String

List of utility methods to do Byte Array to String

Description

The list of methods to do Byte Array to String are organized into topic(s).

Method

StringbytesToString(final byte[] data)
bytes To String
if (data == null) {
    return null;
return bytesToString(data, 0, data.length);
StringbytesToString(int bytes)
Takes a given int representing a size in bytes and converts it to a String representing the size.
int kilobyte = 1024;
int megabyte = kilobyte * kilobyte;
String result = null;
if (bytes >= megabyte)
    result = String.format("%.2f", reduce(bytes, megabyte));
else if (bytes >= kilobyte)
    result = String.format("%.2f", reduce(bytes, kilobyte));
else
...
StringbytesToString(int bytes)
bytes To String
if (bytes < BYTES_IN_1_KILOBYTE) {
    return bytes + " bytes";
} else if (bytes < BYTES_IN_1_MEGABYTE) {
    float kiloBytes = ((float) bytes) / BYTES_IN_1_KILOBYTE;
    return String.format("%.2f kilobytes", kiloBytes);
} else {
    float megaBytes = ((float) bytes) / BYTES_IN_1_MEGABYTE;
    return String.format("%.2f megabytes", megaBytes);
...
StringbytesToString(int... bytesInt)
bytes To String
String s = "";
byte[] bytes = new byte[bytesInt.length];
try {
    int i = 0;
    for (int vlr : bytesInt) {
        bytes[i] = (byte) vlr;
        i++;
    s = new String(bytes);
} catch (Exception e) {
    s = "";
return s;
StringbytesToString(long b)
bytes To String
double gb = (double) b / (1024 * 1024 * 1024);
if (gb >= 1)
    return gb >= 10 ? (int) gb + "G" : round(gb, 1) + "G";
double mb = (double) b / (1024 * 1024);
if (mb >= 1)
    return mb >= 10 ? (int) mb + "M" : round(mb, 1) + "M";
double kb = (double) b / (1024);
if (kb >= 1)
...
StringbytesToString(long bytes)
Returns a human-readable representation of a given number of 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);
StringbytesToString(long bytes)
bytes To String
int unit = 1024;
if (bytes < unit)
    return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = ("KMGTPE").charAt(exp - 1) + "";
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
StringbytesToString(long size)
Convert a quantity in bytes to a human-readable string such as "4.0 MB".
long TB = 1L << 40;
long GB = 1L << 30;
long MB = 1L << 20;
long KB = 1L << 10;
double value;
String unit;
if (size >= 2 * TB) {
    value = (double) size / TB;
...
StringbyteTo16String(byte[] bt)
byte To String
String res = "";
for (int i = 0; i < bt.length; i++) {
    int hex = (int) bt[i] & 0xff;
    System.out.print(Integer.toHexString(hex) + " ");
    res = res + Integer.toHexString(hex);
return res;
StringbyteToMacString(byte[] data)
byte To Mac String
return hexStringToColonSeparatedString(bytesToHexString(data));