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

public static String getMD5Value(String value) {
    byte[] hash;/* ww w  .j  av a 2 s.c o m*/
    try {
        hash = MessageDigest.getInstance(MD5).digest(value.getBytes(UTF8));
    } 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

public static String MD5(String inStr) {
    MessageDigest md5 = null;//ww w .java2s .  c om
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
        return "";
    }
    char[] charArray = inStr.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 hexColorFromRessourceColor(Context context, int idColor) {
    return "#" + Integer.toHexString(context.getResources().getColor(idColor) & 0x00ffffff);
}

From source file:Main.java

public static String getMD5(String inStr) {
    MessageDigest md5;/*from w  ww.  j  a  v a2 s. c o  m*/
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }

    char[] charArray = inStr.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);

    StringBuilder hexValue = new StringBuilder();
    for (byte md5Byte : md5Bytes) {
        int val = ((int) md5Byte) & 0xff;
        if (val < 16) {
            hexValue.append("0");
        }
        hexValue.append(Integer.toHexString(val));
    }
    return hexValue.toString();
}

From source file:Main.java

/**
 * Hex format.//from   www. j a v  a2  s  .  c  o  m
 * 
 * @param i
 *          a int
 * @param j
 *          a int
 * @return a String
 */
private static String hexFormat(int i, int j) {
    final String s = Integer.toHexString(i);
    return padHex(s, j) + s;
}

From source file:Main.java

public static String computeMD5(byte[] input) {
    try {//from   w  w  w .  j  a  va 2 s  .  co  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input, 0, input.length);
        byte[] md5bytes = md.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < md5bytes.length; i++) {
            String hex = Integer.toHexString(0xff & md5bytes[i]);
            if (hex.length() == 1)
                hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String md5Summary(String str) {

    if (str == null) {
        return null;
    }//from w ww  .j  ava2 s  . c o  m

    MessageDigest messageDigest = null;

    try {
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("utf-8"));
    } catch (NoSuchAlgorithmException e) {

        return str;
    } catch (UnsupportedEncodingException e) {
        return str;
    }

    byte[] byteArray = messageDigest.digest();

    StringBuffer md5StrBuff = new StringBuffer();

    for (int i = 0; i < byteArray.length; i++) {
        if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
            md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
        else
            md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
    }

    return md5StrBuff.toString();
}

From source file:Main.java

private static String bytesToHexString(byte[] bytes) {
    // http://stackoverflow.com/questions/332079
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.length; i++) {
        String hex = Integer.toHexString(0xFF & bytes[i]);
        if (hex.length() == 1) {
            sb.append('0');
        }//  w  w  w  .  jav  a  2s  .com
        sb.append(hex);
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Split uint8 to 2 bytes of high byte and low byte. e.g. 20 = 0x14 should
 * be split to [0x01,0x04] 0x01 is high byte and 0x04 is low byte
 * /*from   ww  w .  j  a  v  a 2s.  com*/
 * @param uint8
 *            the char(uint8)
 * @return the high and low bytes be split, byte[0] is high and byte[1] is
 *         low
 */
public static byte[] splitUint8To2bytes(char uint8) {
    if (uint8 < 0 || uint8 > 0xff) {
        throw new RuntimeException("Out of Boundary");
    }
    String hexString = Integer.toHexString(uint8);
    byte low;
    byte high;
    if (hexString.length() > 1) {
        high = (byte) Integer.parseInt(hexString.substring(0, 1), 16);
        low = (byte) Integer.parseInt(hexString.substring(1, 2), 16);
    } else {
        high = 0;
        low = (byte) Integer.parseInt(hexString.substring(0, 1), 16);
    }
    byte[] result = new byte[] { high, low };
    return result;
}

From source file:Main.java

private static String getDigestStr(byte[] origBytes) {
    String tempStr = null;//from  ww  w.jav  a2 s . c o m
    StringBuilder stb = new StringBuilder();
    for (int i = 0; i < origBytes.length; i++) {
        tempStr = Integer.toHexString(origBytes[i] & 0xff);
        if (tempStr.length() == 1) {
            stb.append("0");
        }
        stb.append(tempStr);

    }
    return stb.toString();
}