Example usage for java.util Formatter format

List of usage examples for java.util Formatter format

Introduction

In this page you can find the example usage for java.util Formatter format.

Prototype

public Formatter format(String format, Object... args) 

Source Link

Document

Writes a formatted string to this object's destination using the specified format string and arguments.

Usage

From source file:org.icelib.Hash.java

private static String encode(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 2);
    Formatter formatter = new Formatter(sb);
    for (byte b : bytes) {
        int val = (int) b & 0xff;
        formatter.format("%02x", val);
    }//from w  ww.j a v a  2 s . c o m
    return sb.toString();
}

From source file:Main.java

public static String transferTP(String oldValue) {
    String newValue = oldValue;/*w w w.  j a v a2 s. co m*/
    float ov = Float.valueOf(oldValue) / 1000;
    Formatter fmt = new Formatter();

    if (ov >= 100) {
        newValue = String.valueOf(Math.floor(ov + 0.5));
    } else if (ov > 10) {
        newValue = fmt.format("%.1f", ov).toString();
    } else {
        newValue = fmt.format("%.2f", ov).toString();
    }

    return newValue;
}

From source file:dk.dma.msinm.user.security.SecurityUtils.java

/**
 * Hex the byte buffer// w  ww.  j a v  a 2 s . co  m
 * @param bytes the bytes to Hex
 * @return the result
 */
public static String hex(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 2);

    Formatter formatter = new Formatter(sb);
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }

    return sb.toString();
}

From source file:edu.smc.mediacommons.modules.Md5Module.java

public static String getSHA1(final File file) {
    String sha1 = null;/* w  ww . j  a  v  a 2  s. c  om*/

    try {
        final MessageDigest messageDigest = MessageDigest.getInstance("SHA1");

        InputStream is = new BufferedInputStream(new FileInputStream(file));
        final byte[] buffer = new byte[1024];
        for (int read = 0; (read = is.read(buffer)) != -1;) {
            messageDigest.update(buffer, 0, read);
        }

        // Convert the byte to hex format
        Formatter formatter = new Formatter();
        for (final byte b : messageDigest.digest()) {
            formatter.format("%02x", b);
        }

        sha1 = formatter.toString();
        formatter.close();
        is.close();
    } catch (IOException | NoSuchAlgorithmException e) {
        e.printStackTrace();
    } finally {

    }

    return sha1;
}

From source file:cc.recommenders.utils.Fingerprints.java

private static String toHexString(final byte[] hash) {
    ensureIsNotNull(hash);/*from w w w  . java 2  s.co  m*/
    // this is said to be very slow... - we may look at this if hashing
    // actually takes too long.
    final Formatter formatter = new Formatter();
    for (final byte b : hash) {
        formatter.format("%02x", b);
    }
    String out = formatter.toString();
    formatter.close();
    return out;
}

From source file:com.cloud.utils.NumbersUtil.java

public static String toReadableSize(long bytes) {
    if (bytes < KB && bytes >= 0) {
        return Long.toString(bytes) + " bytes";
    }//from w w w. ja v  a 2s .  co m
    StringBuilder builder = new StringBuilder();
    Formatter format = new Formatter(builder, Locale.getDefault());
    if (bytes < MB) {
        format.format("%.2f KB", (float) bytes / (float) KB);
    } else if (bytes < GB) {
        format.format("%.2f MB", (float) bytes / (float) MB);
    } else if (bytes < TB) {
        format.format("%.2f GB", (float) bytes / (float) GB);
    } else {
        format.format("%.4f TB", (float) bytes / (float) TB);
    }
    format.close();
    return builder.toString();
}

From source file:com.codedx.burp.security.InvalidCertificateDialogStrategy.java

public static String toHexString(byte[] bytes, String sep) {
    Formatter f = new Formatter();
    for (int i = 0; i < bytes.length; i++) {
        f.format("%02x", bytes[i]);
        if (i < bytes.length - 1) {
            f.format(sep);//from  w w w.j  a v a  2s  . c o m
        }
    }
    String result = f.toString();
    f.close();
    return result;
}

From source file:org.docwhat.iated.EditSession.java

/** Method to convert bytes to a String.
 *
 * @param hash A set of bytes//from   w ww.j ava  2  s  .c  om
 * @return a hex encoded String.
 */
private static String byteArray2Hex(byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}

From source file:com.codealot.textstore.FileStore.java

private static String byteToHex(final byte[] hash) {
    final Formatter formatter = new Formatter();
    for (final byte b : hash) {
        formatter.format("%02X", b);
    }/*w w  w  .ja  va 2 s .c o  m*/
    final String result = formatter.toString();
    formatter.close();
    return result;
}

From source file:Main.java

public static String byteArrayToHexString(byte[] paramArrayOfByte) {
    StringBuilder localStringBuilder = new StringBuilder(2 * paramArrayOfByte.length);
    Formatter localFormatter = new Formatter(localStringBuilder);
    int i = paramArrayOfByte.length;
    for (int j = 0; j < i; j++) {
        byte b = paramArrayOfByte[j];
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = Byte.valueOf(b);
        localFormatter.format("%02x", arrayOfObject);
    }/*from  w ww  .  j a  va  2  s.co  m*/
    localFormatter.close();
    return localStringBuilder.toString();
}