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 getMD5Str(String str) {
    MessageDigest messageDigest = null;
    try {/*from ww w  .  ja v a 2s. co  m*/
        messageDigest = MessageDigest.getInstance("MD5");

        messageDigest.reset();

        messageDigest.update(str.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        System.out.println("NoSuchAlgorithmException caught!");
    } catch (UnsupportedEncodingException e) {
    }

    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.substring(0, 24).toString().toUpperCase();
}

From source file:Main.java

public static String toUnicode(String input) {
    StringBuffer ret = new StringBuffer();
    if (input != null) {
        for (int i = 0; i < input.length(); i++) {
            char ch = input.charAt(i);
            if (!Character.isWhitespace(ch) && ch < 0x20 || ch > 0x7e) {
                ret.append("\\u");
                // requires 1.5 VM
                // ret.append(String.format("%1$04x", new Object[] { Integer.valueOf(ch) }));
                ret.append(leading4Zeros(Integer.toHexString(ch)));
            } else {
                ret.append(ch);/*from  w w  w .  j  av  a  2s  .  c  om*/
            }
        }
    }
    return ret.toString();
}

From source file:Main.java

static String md5hash(String key) {
    MessageDigest hash = null;/* w ww  . java 2 s . com*/
    try {
        hash = MessageDigest.getInstance(HASH_ALGORITHM_MD5);
    } catch (NoSuchAlgorithmException e) {
        return null;
    }

    hash.update(key.getBytes());
    byte[] digest = hash.digest();
    StringBuilder builder = new StringBuilder();
    for (int b : digest) {
        builder.append(Integer.toHexString((b >> 4) & 0xf));
        builder.append(Integer.toHexString((b >> 0) & 0xf));
    }
    return builder.toString();
}

From source file:Main.java

public static String toHexString(byte[] value, int startOffset, int maxLength, boolean uppercase,
        char separator) {
    if (maxLength == -1 || startOffset + maxLength > value.length) {
        maxLength = value.length - startOffset;
    }// ww  w  . j a v a2  s .c om
    StringBuffer r = new StringBuffer(maxLength * (separator == -1 ? 2 : 3));
    for (int i = 0; i < maxLength; i++) {
        if (i > 0 && separator != 0) {
            r.append(separator);
        }
        String t = Integer.toHexString(value[i + startOffset] & 0xFF);
        if (t.length() == 1) {
            t = "0" + t;
        }
        if (uppercase) {
            t = t.toUpperCase();
        }
        r.append(t);
    }
    return r.toString();
}

From source file:Main.java

/**
 * Gets the hex string./*from w  w  w.  j av  a  2  s  .  co m*/
 *
 * @param data the data
 * @param offset the offset
 * @param len the len
 * @param delimiter the delimiter
 * @return the hex string
 */
public static String getHexString(byte[] data, int offset, int len, String delimiter) {
    if (data != null) {
        StringBuffer str = new StringBuffer(len);
        for (int i = 0; i < len; i++) {
            if (i != 0 && i % 16 == 0) {
                //         str.append("\n  ");
            }
            String digit = Integer.toHexString((data[i + offset] & 0x00ff));
            if (digit.length() == 1)
                digit = '0' + digit;
            digit = digit.toUpperCase();
            str.append(digit + delimiter);
        }
        return str.toString();
    }
    return "";
}

From source file:Main.java

/**
 * Return a hex string of the contents of buffer.
 * @param buffer The buffer.//from www .  j a  v a2s  .  c om
 * @return A string of hex bytes.
 */
public static String toHex(byte[] buffer) {
    StringBuffer output = new StringBuffer(buffer.length * 2);
    for (int i = 0; i < buffer.length; ++i) {
        String hex = Integer.toHexString((int) buffer[i] & 0xff);
        if (hex.length() <= 1)
            // Append the leading zero.
            output.append("0");
        output.append(hex);
    }

    return output.toString();
}

From source file:Main.java

public static String byteArrayToHexString(final byte[] b) {
    if (b == null || b.length == 0)
        return null;
    final StringBuffer sb = new StringBuffer(b.length * 3 - 1);
    for (final byte element : b) {
        if (sb.length() > 0)
            sb.append(':');
        final int v = element & 0xff;
        if (v < 16)
            sb.append('0');
        sb.append(Integer.toHexString(v));
    }/*w w w  .ja v a2 s . c o  m*/
    return sb.toString().toUpperCase(Locale.US);
}

From source file:Main.java

public static String generateMD5(String value) {
    try {/*ww  w.j a  va 2  s.c  o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bytes;
        try {
            bytes = value.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e1) {
            bytes = value.getBytes();
        }
        StringBuilder result = new StringBuilder();
        for (byte b : md.digest(bytes)) {
            result.append(Integer.toHexString((b & 0xf0) >>> 4));
            result.append(Integer.toHexString(b & 0x0f));
        }
        return result.toString();
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

private static String getLockInfo(ReentrantReadWriteLock lock) {
    String lockid = "RWLock@" + Integer.toHexString(lock.hashCode());
    return lockid + " readLockCount=" + lock.getReadLockCount() + " isWriteLocked=" + lock.isWriteLocked();
}

From source file:Main.java

final static String encode(String s) throws UnsupportedEncodingException {
    byte buf[] = s.getBytes("utf-8");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < buf.length; i++) {
        int cur = (buf[i] & 0xff);
        if (isReserved(cur)) {
            sb.append("%");
            if (cur < 0x10) {
                sb.append("0");
            }/*  w  w w .j a  v a 2  s.com*/
            sb.append(Integer.toHexString(cur).toUpperCase());
        } else {
            sb.append((char) (cur));
        }
    }
    return sb.toString();
}