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:Main.java

public static String toHexString(byte[] bytes) {
    // Used to parse byte array and return as string
    StringBuilder sb = new StringBuilder(bytes.length * 2);

    Formatter formatter = new Formatter(sb);
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }//  w ww. j  a va  2s .co m
    formatter.close();
    return sb.toString();
}

From source file:Main.java

public static String hexEncode(byte[] in) {
    StringBuilder builder = new StringBuilder(in.length * 2);

    Formatter formatter = new Formatter(builder);
    for (byte inByte : in) {
        formatter.format("%02x", inByte);
    }/*from w w  w . j a v  a 2 s . co m*/

    return builder.toString();
}

From source file:Main.java

private static String mBytesToHexString(byte[] bytes) {
    StringBuilder sb = new StringBuilder(bytes.length * 2);

    Formatter formatter = new Formatter(sb);
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }/*from  ww w  .j  av  a 2  s.c  o  m*/

    return "0x" + sb.toString().toUpperCase();
}

From source file:Main.java

public static String toSHA1(String s) {
    MessageDigest md = null;/*from  ww  w  .j  a  v a 2s . c o m*/
    byte[] sha1hash = null;
    try {
        md = MessageDigest.getInstance("SHA-1");
        sha1hash = new byte[40];
        md.update(s.getBytes(UTF_8), 0, s.length());
    } catch (Exception e) {
        return ERROR_SHA1;
    }
    sha1hash = md.digest();
    Formatter formatter = new Formatter();
    for (byte b : sha1hash) {
        formatter.format("%02x", b);
    }
    return formatter.toString();
}

From source file:Main.java

public static String toUUIDFormat(byte[] bytes) {
    byte[] switched = new byte[] { bytes[3], bytes[2], bytes[1], bytes[0], bytes[5], bytes[4], bytes[7],
            bytes[6], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15] };

    StringBuilder sb = new StringBuilder(bytes.length * 2);
    Formatter formatter = new Formatter(sb);
    for (byte b : switched) {
        formatter.format("%02x", b);
    }//from  ww w. ja v a2  s .  c  o  m
    sb.insert(8, "-");
    sb.insert(13, "-");
    sb.insert(18, "-");
    sb.insert(23, "-");

    String temp = sb.toString().toUpperCase();
    formatter.close();
    return temp;
}

From source file:com.legstar.mock.client.MockVararcom.java

/**
 * @return a hexadecimal representation of host data.
 *//*from  w  ww .j  a  v  a2  s.c  o m*/
public static String getHostBytesHex36() {

    StringBuilder sb = new StringBuilder("0024");
    for (int i = 0; i < 36; i++) {
        StringBuilder sbl = new StringBuilder();
        Formatter formatter = new Formatter(sbl, Locale.US);
        formatter.format("%04x", 5 * (i + 1));
        sb.append("c6c7c8c9d1" + sbl.toString());
    }

    return sb.toString();
}

From source file:ch.unibe.cde.geonet.kernel.security.Md5PasswordEncoder.java

/**
 *
 * @param hash//from   ww w. j  ava 2s  . co m
 * @return
 *
 * Copied from http://stackoverflow.com/a/9071224/1829038
 */
private static String byteToHex(final byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash) {
        formatter.format("%02x", b);
    }
    String result = formatter.toString();
    formatter.close();
    return result;
}

From source file:Main.java

public static String showSirfCommandFromPayload(String payload) {
    byte[] command = genSirfCommandFromPayload(payload);
    StringBuilder out = new StringBuilder(payload.length() + 16);
    Formatter fmt = new Formatter(out, null);
    for (byte b : command) {
        fmt.format("%02X", b);
    }//from w  w  w.j  ava 2 s . co m
    return out.toString();
}

From source file:com.opentok.test.Helpers.java

private static String toHexString(byte[] bytes) {
    Formatter formatter = new Formatter();
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }/* ww  w .ja va2 s.  com*/
    return formatter.toString();
}

From source file:Main.java

/**
 * Returns the Md5 hash for the given text.
 * //from w w w  .ja v  a  2  s . c o  m
 * @param text
 *            String whose Md5 hash should be returned.
 * @return Returns the Md5 hash for the given text.
 */
public static String getMd5Hash(String text) {
    StringBuffer result = new StringBuffer(32);
    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(text.getBytes());
        Formatter f = new Formatter(result);

        byte[] digest = md5.digest();

        for (int i = 0; i < digest.length; i++) {
            f.format("%02x", new Object[] { new Byte(digest[i]) });
        }
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }

    return result.toString();
}