Example usage for java.lang Integer toHexString

List of usage examples for java.lang Integer toHexString

Introduction

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

Prototype

public static String toHexString(int i) 

Source Link

Document

Returns a string representation of the integer argument as an unsigned integer in base 16.

Usage

From source file:Main.java

private static String byte2Hex(byte[] b) {
    StringBuilder sb = new StringBuilder();
    for (byte aB : b) {
        String s = Integer.toHexString(aB & 0xFF);
        if (s.length() == 1) {
            sb.append("0");
        }/*from   ww  w .j av a2s.c  o  m*/
        //sb.append(s.toUpperCase());
        sb.append(s);
    }
    return sb.toString();
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash;//from  w ww.  j  a v a  2 s.  com

    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);

    for (byte b : hash) {
        int i = (b & 0xFF);
        if (i < 0x10)
            hex.append('0');
        hex.append(Integer.toHexString(i));
    }

    return hex.toString();
}

From source file:Main.java

public static String byte2HexStr(byte[] b, int length) {
    String hs = "";
    String stmp = "";
    for (int n = 0; n < length; ++n) {
        stmp = Integer.toHexString(b[n] & 0xFF);
        if (stmp.length() == 1)
            hs = hs + "0" + stmp;
        else {/*from  www  .  ja  v a  2 s . co  m*/
            hs = hs + stmp;
        }
        hs = hs + ",";
    }
    return hs.toUpperCase();
}

From source file:Main.java

public static String md5(String string) {
    byte[] hash;/*w  w  w  .  j  a v  a  2 s.c  om*/
    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}

From source file:Main.java

/**
 * Takes the provided byte array and converts it into a hexadecimal string
 * with two characters per byte.//from  ww w  . ja  v a 2s . c om
 *
 * @param withSpaces if true, include a space character between each hex-rendered
 *                   byte for readability.
 */
public static String bytesToHex(byte[] bytes, boolean withSpaces) {
    StringBuilder sb = new StringBuilder();
    for (byte hashByte : bytes) {
        int intVal = 0xff & hashByte;
        if (intVal < 0x10) {
            sb.append('0');
        }
        sb.append(Integer.toHexString(intVal));
        if (withSpaces) {
            sb.append(' ');
        }
    }
    return sb.toString();
}

From source file:Main.java

private static void writeHexByte(StringBuilder sb, int b) {
    if (b < 0x10) {
        sb.append('0');
    }/*from w w  w. ja va 2s .com*/

    sb.append(Integer.toHexString(b).toUpperCase());
}

From source file:Main.java

public static String printBytes(byte[] data) {
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    for (int i = 0; i < data.length; i++) {
        String hex = Integer.toHexString(data[i]);
        if (hex.length() == 1)
            hex = "0" + hex;
        else/*ww w.j a va  2  s .c  o  m*/
            hex = hex.substring(hex.length() - 2);
        sb.append(hex);
        if (i < data.length - 1) {
            if ((i + 1) % 30 == 0)
                sb.append("\n ");
            else if ((i + 1) % 10 == 0)
                sb.append("  ");
            else
                sb.append(" ");
        }
    }
    sb.append("]");
    return sb.toString();
}

From source file:Main.java

public static String printBytes(byte[] data) {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (int i = 0; i < data.length; i++) {
        String hex = Integer.toHexString(data[i]);
        if (hex.length() == 1)
            hex = "0" + hex;
        else/*from  w  ww  .j a  va2  s .  c  o m*/
            hex = hex.substring(hex.length() - 2);
        sb.append(hex);
        if (i < data.length - 1) {
            if ((i + 1) % 30 == 0)
                sb.append("\n ");
            else if ((i + 1) % 10 == 0)
                sb.append("  ");
            else
                sb.append(" ");
        }
    }
    sb.append("]");
    return sb.toString();
}

From source file:Main.java

public static String MD5(String str) {
    MessageDigest md5 = null;/*from  www  .j ava2  s.c  o m*/
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }

    char[] charArray = str.toCharArray();
    byte[] byteArray = new byte[charArray.length];
    for (int i = 0; i < charArray.length; i++) {
        byteArray[i] = (byte) charArray[i];
    }

    byte[] md5Bytes = md5.digest(byteArray);
    StringBuffer hexValue = new StringBuffer();
    for (int i = 0; i < md5Bytes.length; i++) {
        int val = ((int) md5Bytes[i]) & 0xff;
        if (val < 16) {
            hexValue.append("0");
        }
        hexValue.append(Integer.toHexString(val));
    }
    return hexValue.toString();
}

From source file:Main.java

public static String sha1(String string) {
    byte[] hash;/*from   w w w  .  j ava2s  .  c o  m*/
    try {
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        sha1.update(string.getBytes("UTF-8"));
        hash = sha1.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, SHA-1 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, SHA-1 UTF-8 should be supported?", e);
    }
    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10)
            hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}