Example usage for java.util Formatter toString

List of usage examples for java.util Formatter toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns the result of invoking toString() on the destination for the output.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Formatter fmt = new Formatter();
    fmt.format("Formatting %s is easy %d %f", "with Java", 10, 98.6);

    System.out.println(fmt.toString());
}

From source file:Main.java

public static void main(String[] args) {

    StringBuffer buffer = new StringBuffer();
    Formatter formatter = new Formatter(buffer, Locale.US);

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string with default locale
    System.out.println(formatter);

    // print the formatter as a string
    System.out.println(formatter.toString());
}

From source file:Main.java

public static String toSHA1(String s) {
    MessageDigest md = null;/* www. j  a  v a 2 s.  c  om*/
    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:ch.unibe.cde.geonet.kernel.security.Md5PasswordEncoder.java

/**
 *
 * @param hash//from  w w  w .  ja  v a2s .  c  om
 * @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:com.opentok.test.Helpers.java

private static String toHexString(byte[] bytes) {
    Formatter formatter = new Formatter();
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }//from w w  w. j ava 2  s  . c  o m
    return formatter.toString();
}

From source file:sturesy.util.Crypt.java

public static String toHex(byte[] bytes) {
    Formatter formatter = new Formatter();
    for (byte b : bytes) {
        formatter.format("%02x", b);
    }//w  ww.ja v  a 2 s.  c om
    String result = formatter.toString();
    formatter.close();
    return result;
}

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

public static String getSHA1(final File file) {
    String sha1 = null;//  www  .jav 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  ww . j  ava  2s.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:pt.webdetails.cdc.hazelcast.CfgTestApp.java

public static String toHexString(byte[] bytes) {
    Formatter formatter = new Formatter();
    try {/*from  w  w  w . j  a v a2 s  . c  om*/
        for (byte b : bytes) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    } finally {
        formatter.close();
    }
}

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

/** Method to convert bytes to a String.
 *
 * @param hash A set of bytes/*from   www. j a  va2 s . c  o m*/
 * @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();
}