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

public static String toHexString(byte[] keyData) {
    if (keyData == null) {
        return null;
    }/* ww w.j  av a 2 s . co  m*/
    int expectedStringLen = keyData.length * 2;
    StringBuilder sb = new StringBuilder(expectedStringLen);
    for (int i = 0; i < keyData.length; i++) {
        String hexStr = Integer.toString(keyData[i] & 0x00FF, 16);
        if (hexStr.length() == 1) {
            hexStr = "0" + hexStr;
        }
        sb.append(hexStr);
    }
    return sb.toString();
}

From source file:Main.java

public static String md5(String str) throws Exception {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(str.getBytes());//from  w  w w .ja v  a 2 s.c  o  m

    byte byteData[] = md.digest();

    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();
}

From source file:MD5StringUtil.java

public static String md5StringFor(String s) {
    final byte[] hash = digest.digest(s.getBytes());
    final StringBuilder builder = new StringBuilder();
    for (byte b : hash) {
        builder.append(Integer.toString(b & 0xFF, 16));
    }/*from   ww  w .  j a  v  a 2 s.  c  om*/
    return builder.toString();
}

From source file:Main.java

/**
 * Returns the byte array as a hex string in the format
 * "0x1234"./* w  w  w . j a  v  a  2 s  .  c  om*/
 * @param data byte array
 * @return hex string
 */
public static String getHexString(byte[] data) {
    StringBuffer hex = new StringBuffer("0x");
    if (data != null)
        for (int i = 0; i < data.length; i++) {
            String digit = Integer.toString(data[i] & 0x0ff, 16);
            if (digit.length() < 2)
                hex.append('0');
            hex.append(digit);
        }
    return hex.toString();
}

From source file:Main.java

public static String byteArr2HexStr(byte[] arrB) throws Exception {
    int iLen = arrB.length;
    StringBuffer sb = new StringBuffer(iLen * 2);

    for (int i = 0; i < iLen; ++i) {
        int intTmp;
        for (intTmp = arrB[i]; intTmp < 0; intTmp += 256) {
            ;/*  w ww  . j  a  v a 2  s .  co m*/
        }
        if (intTmp < 16) {
            sb.append("0");
        }
        sb.append(Integer.toString(intTmp, 16).toUpperCase());
    }
    return sb.toString();
}

From source file:Main.java

public static String toHex(byte input[]) {
    if (input == null)
        return null;
    StringBuffer output = new StringBuffer(input.length * 2);
    for (int i = 0; i < input.length; i++) {
        int current = input[i] & 0xff;
        if (current < 16)
            output.append("0");
        output.append(Integer.toString(current, 16));
    }/* ww w.j a  v  a2  s. c  om*/

    return output.toString();
}

From source file:Main.java

public static String hash(String pass) {
    MessageDigest md = null;// w  ww  .j a  v  a2 s.  c om
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    md.update(pass.getBytes());
    byte byteData[] = md.digest();
    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();

}

From source file:Main.java

private static String computeHash(String input) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.reset();/*from   w  w  w . j  a  v  a 2  s.c  o m*/

    byte[] byteData = digest.digest(input.getBytes("UTF-8"));
    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();
}

From source file:Main.java

public static String getHash(String input) {
    if (input == null || input.equals("") || input.isEmpty()) {
        return "";
    }/* ww  w .j a v a 2  s. c o  m*/

    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] messageDigest = md.digest(input.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            sb.append(Integer.toString((messageDigest[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String sha512(String what_to_encode) {
    final MessageDigest sha512;
    try {/*w w w  .j  a  va  2 s .c  o m*/
        sha512 = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
        return "404";
    }
    sha512.update(what_to_encode.getBytes());
    byte byteData[] = sha512.digest();
    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();
}