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 DecodeUnicodeHex(char[] Char) {
    String strTemp = "";
    boolean CouldContinue = true;

    for (int i = Char.length - 1; i >= 0; i--) {
        String strHex = Integer.toHexString((int) Char[i]);

        if (strHex.length() == 1)
            strHex = "0" + strHex;

        if (strHex.equals("00") && CouldContinue)
            continue;
        else {/*from w ww. ja v  a 2s  .co m*/
            strTemp += strHex;
            CouldContinue = false;
        }
    }

    return strTemp;
}

From source file:Main.java

public static String getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {//from   ww  w  .  jav a2 s .c om
        messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    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

public static String getMD5Str(String md5Str) {
    byte[] hash;//  ww  w  . ja va 2s . c  o  m
    try {
        hash = MessageDigest.getInstance("MD5").digest(md5Str.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

public static String MD5(String string) {
    String result = null;/*from  w ww  . j a v  a2  s.  c om*/
    try {
        char[] charArray = string.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }

        StringBuffer hexValue = new StringBuffer();
        byte[] md5Bytes = MessageDigest.getInstance("MD5").digest(byteArray);
        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));
        }

        result = hexValue.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String byteArrayToHex(byte[] array, int offset, int length) {
    StringBuilder sb = new StringBuilder();

    sb.append("---\n");

    boolean isFirst = true;
    int count = 0;
    for (int i = offset; i < offset + length; i++) {
        byte b = array[i];

        if (!isFirst) {
            if (count == 16) {
                count = 0;/*from  w w  w .j a v a2s.  c  o m*/
                sb.append('\n');
            } else {
                sb.append(' ');
            }
        } else {
            isFirst = false;
        }

        String a = Integer.toHexString(((int) b) & 0xff);
        if (a.length() == 1)
            a = '0' + a;
        sb.append(a);

        count++;
    }

    sb.append("\n---\n");

    return sb.toString();
}

From source file:Main.java

private static String byte2hex(byte[] bytes) {
    StringBuilder sign = new StringBuilder();
    for (byte data : bytes) {
        String hex = Integer.toHexString(data & 0xFF);
        if (hex.length() == 1) {
            sign.append("0");
        }/*from   w w  w  . ja  v a2  s .  c  om*/
        sign.append(hex.toUpperCase());
    }
    return sign.toString();
}

From source file:Main.java

private static String encodingSpecUrlChar(char c) {
    int asciiCode = (int) c;
    String hexStr = Integer.toHexString(asciiCode).toUpperCase(Locale.ENGLISH);
    if (hexStr.length() < 2) {
        hexStr = "0" + hexStr;
    }/*from  w w  w.j a v  a2s. c  o  m*/

    String result = "%" + hexStr;
    return result;
}

From source file:Main.java

public static String md5(String string) {

    byte[] hash;/* w ww .  j av a2s.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) {

        if ((b & 0xFF) < 0x10)
            hex.append("0");

        hex.append(Integer.toHexString(b & 0xFF));

    }

    return hex.toString();

}

From source file:Main.java

public static String byte2hex(byte buffer) {
    String h = Integer.toHexString(buffer & 0xFF);
    if (h.length() == 1) {
        h = "0" + h;
    }/* w  w w .  j  a v  a2  s  .c o  m*/
    return h;
}

From source file:MD5.java

public static String crypt(String str) {
    if (str == null || str.length() == 0) {
        throw new IllegalArgumentException("String to encript cannot be null or zero length");
    }//from w  w w  .  j av  a2 s.  c  o m

    digester.update(str.getBytes());
    byte[] hash = digester.digest();
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < hash.length; i++) {
        if ((0xff & hash[i]) < 0x10) {
            hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
        } else {
            hexString.append(Integer.toHexString(0xFF & hash[i]));
        }
    }
    return hexString.toString();
}