Example usage for java.lang Integer toString

List of usage examples for java.lang Integer toString

Introduction

In this page you can find the example usage for java.lang Integer toString.

Prototype

public static String toString(int i, int radix) 

Source Link

Document

Returns a string representation of the first argument in the radix specified by the second argument.

Usage

From source file:Main.java

/**
 * //from  w  w  w. jav  a  2 s .c o m
 * @param word
 *            the string to be hashed
 * @return SHA1-hash of the word
 */
public static String hash(String word) {
    byte[] data = word.getBytes();
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    byte[] b = md.digest(data);
    String result = "";
    for (int i = 0; i < b.length; i++) {
        result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    return result;
}

From source file:Main.java

public static String md5hash(String string) {
    String generatedString = null;
    try {/*from   w  w w.ja  v a  2  s. c  o  m*/
        // Create MessageDigest instance for MD5
        MessageDigest md = MessageDigest.getInstance("MD5");
        // Add password bytes to digest
        md.update(string.getBytes());
        // Get the hash's bytes
        byte[] bytes = md.digest();
        // This bytes[] has bytes in decimal format;
        // Convert it to hexadecimal format
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
        // Get complete hashed password in hex format
        generatedString = sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return generatedString;
}

From source file:Main.java

private static String getHexString(byte[] data) {
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        stringBuffer.append(Integer.toString((data[i] & 0xff) + 0x100, 16).substring(1));
    }//from ww  w  . ja va  2  s .c o m
    return stringBuffer.toString();
}

From source file:Main.java

public static String toHexString(byte b) {
    StringBuffer result = new StringBuffer(3);
    result.append(Integer.toString((b & 0xF0) >> 4, 16));
    result.append(Integer.toString(b & 0x0F, 16));
    return result.toString();
}

From source file:Main.java

private static String sha256Hex(final String filePath) throws NoSuchAlgorithmException, IOException {
    final InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
    final MessageDigest md = MessageDigest.getInstance("SHA-256");

    final byte[] dataBytes = new byte[1024];

    int nread;/*  ww  w.  j a va 2 s . c o  m*/
    while ((nread = fis.read(dataBytes)) != -1)
        md.update(dataBytes, 0, nread);
    final byte[] mdbytes = md.digest();

    final StringBuilder sb = new StringBuilder();
    for (final byte b : mdbytes)
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

    return sb.toString();
}

From source file:Main.java

public static String getMD5(String file) {
    String md5 = "";

    try {/*  w  w w .  jav  a  2 s.c  om*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        InputStream is;
        is = new FileInputStream(file);

        DigestInputStream dis = new DigestInputStream(is, md);
        byte data[] = new byte[1024];
        @SuppressWarnings("unused")
        int count;
        while ((count = dis.read(data)) != -1) {

        }
        byte[] digest = md.digest();

        for (int i = 0; i < digest.length; i++) {
            md5 += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
        }
        return md5;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return md5;
}

From source file:Main.java

public static String escapeXML(final String text) {
    final StringBuilder sb = new StringBuilder();
    final int len = text.length();
    for (int i = 0; i < len; i++) {
        final char c = text.charAt(i);
        switch (c) {
        case 34:/*from w  ww .  j  a  v  a 2  s  . co  m*/
            sb.append("&quot;");
            break;
        case 38:
            sb.append("&amp;");
            break;
        case 39:
            sb.append("&apos;");
            break;
        case 60:
            sb.append("&lt;");
            break;
        case 62:
            sb.append("&gt;");
            break;
        default:
            if (c > 0x7F) {
                sb.append("&#");
                sb.append(Integer.toString(c, 10));
                sb.append(';');
            } else {
                sb.append(c);
            }
        }
    }
    return sb.toString();
}

From source file:Main.java

public static void writeXML(PrintWriter out, String str) {
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);

        switch (ch) {
        case '<':
            out.write("&lt;");
            break;

        case '>':
            out.write("&gt;");
            break;

        case '&':
            out.write("&amp;");
            break;

        case '"':
            out.write("&quot;");
            break;

        case '\'':
            out.write("&apos;");
            break;

        case '\r':
        case '\n':
            out.write(ch);// w  w w . ja  va  2s. c  om
            break;

        default:
            if ((ch < 32) || (ch > 126)) {
                out.write("&#x");
                out.write(Integer.toString(ch, 16));
                out.write(';');
            } else {
                out.write(ch);
            }
            break;
        }
    }
}

From source file:com.smallfe.clerk.util.DigestUtil.java

public static String getSHA256(String message) {
    try {/*from  ww  w  .  ja v a  2 s . co  m*/
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(message.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(DigestUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:Main.java

public static String SHA1(String input) throws NoSuchAlgorithmException {
    MessageDigest mDigest = MessageDigest.getInstance("SHA1");
    byte[] result = mDigest.digest(input.getBytes());
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < result.length; i++) {
        sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
    }//  w w w. jav  a 2 s.  c om

    return sb.toString();
}