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:com.zen.androidhtmleditor.util.TextUtil.java

public static String MD5(String str, String encoding) {
    MessageDigest messageDigest = null;

    try {/*from   ww  w .  j  a  v a2 s  .co m*/
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    messageDigest.reset();
    try {
        messageDigest.update(str.getBytes(encoding));
    } 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 toHex(byte[] data) {
    StringBuffer hash = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        String hex = Integer.toHexString(0xFF & data[i]);
        if (hex.length() == 1) {
            hash.append('0');
        }/*w  w w  .  j  a  v  a  2s  .com*/
        hash.append(hex);
    }
    return hash.toString();
}

From source file:com.feihua.utils.digest.DigestUtils.java

public static String md5(String input) {
    byte[] md5Bytes = md5(input.getBytes());
    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));
    }/*from w ww.ja  v  a 2 s  .  com*/
    return hexValue.toString();
}

From source file:Main.java

/**
  Convert a byte array to a human-readable String for debugging purposes.
*//*w w  w.j a  v  a  2s . c  o m*/
public static String hexDump(byte[] data) {
    byte byte_value;

    StringBuffer str = new StringBuffer(data.length * 3);

    str.append("Hex dump:\n");

    for (int i = 0; i < data.length; i += 16) {
        // dump the header: 00000000: 
        String offset = Integer.toHexString(i);

        // "0" left pad offset field so it is always 8 char's long.
        for (int offlen = offset.length(); offlen < 8; offlen++)
            str.append("0");
        str.append(offset);
        str.append(":");

        // dump hex version of 16 bytes per line.
        for (int j = 0; (j < 16) && ((i + j) < data.length); j++) {
            byte_value = data[i + j];

            // add spaces between every 2 bytes.
            if ((j % 2) == 0)
                str.append(" ");

            // dump a single byte.
            byte high_nibble = (byte) ((byte_value & 0xf0) >>> 4);
            byte low_nibble = (byte) (byte_value & 0x0f);

            str.append(hex_table[high_nibble]);
            str.append(hex_table[low_nibble]);
        }

        // dump ascii version of 16 bytes
        str.append("  ");

        for (int j = 0; (j < 16) && ((i + j) < data.length); j++) {
            char char_value = (char) data[i + j];

            // RESOLVE (really want isAscii() or isPrintable())
            if (Character.isLetterOrDigit(char_value))
                str.append(String.valueOf(char_value));
            else
                str.append(".");
        }

        // new line
        str.append("\n");
    }
    return (str.toString());

}

From source file:Main.java

@SuppressWarnings("TryWithIdenticalCatches")
public static LayoutAnimationController loadLayoutAnimation(Context context, int id) throws NotFoundException {

    XmlResourceParser parser = null;//from  ww  w  . j av a2s .c o m
    try {
        parser = context.getResources().getAnimation(id);
        return createLayoutAnimationFromXml(context, parser);
    } catch (XmlPullParserException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null)
            parser.close();
    }
}

From source file:Main.java

private static final String bytesToHexString(byte[] bArr) {
    StringBuilder stringBuilder = new StringBuilder();
    if (bArr == null || bArr.length <= 0) {
        return null;
    }//from   w ww .  j  a v a2 s  .  co m
    for (int i = SYSTEM_ROOT_STATE_DISABLE; i < bArr.length; i += SYSTEM_ROOT_STATE_ENABLE) {
        String toHexString = Integer.toHexString(bArr[i] & 255);// TODO 255
        if (toHexString.length() < 2) {
            stringBuilder.append(SYSTEM_ROOT_STATE_DISABLE);
        }
        stringBuilder.append(toHexString);
    }
    return stringBuilder.toString();
}

From source file:com.netflix.exhibitor.core.s3.S3Utils.java

public static String toHex(byte[] digest) {
    StringBuilder sb = new StringBuilder(digest.length * 2);
    for (byte b : digest) {
        String hex = Integer.toHexString(b);
        if (hex.length() == 1) {
            sb.append("0");
        } else if (hex.length() == 8) {
            hex = hex.substring(6);//from   w  w w.j  av a 2s.  c  o m
        }
        sb.append(hex);
    }
    return sb.toString().toLowerCase();
}

From source file:Main.java

/**
 * VLC authorize only "-._~" in Mrl format, android Uri authorize "_-!.~'()*".
 * Therefore, encode the characters authorized by Android Uri when creating a mrl from an Uri.
 *//*w w w  .jav  a  2s.c o  m*/
public static String locationFromUri(Uri uri) {
    final char array[] = uri.toString().toCharArray();
    final StringBuilder sb = new StringBuilder(array.length * 2);

    for (final char c : array) {
        if (URI_AUTHORIZED_CHARS.indexOf(c) != -1)
            sb.append("%").append(Integer.toHexString(c));
        else
            sb.append(c);
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Formats a given nonce count as a HTTP header value. The header is
 * {@link org.restlet.engine.header.HeaderConstants#HEADER_AUTHENTICATION_INFO}.
 * //from   w w w.jav a 2 s  .c  o  m
 * @param nonceCount
 *            The given nonce count.
 * @return The formatted value of the given nonce count.
 */
public static String formatNonceCount(int nonceCount) {
    StringBuilder result = new StringBuilder(Integer.toHexString(nonceCount));
    while (result.length() < 8) {
        result.insert(0, '0');
    }

    return result.toString();
}

From source file:CryptPassword.java

/**
 * Generates the MD5 hash from a given string.
 * /* ww w  . j av a  2s. co  m*/
 * @param inputString
 *            is the input String
 * @return the MD5 from the string used.
 */
public static String getMD5Hash(String inputString) {
    byte buf[] = inputString.getBytes();
    StringBuffer hexString = new StringBuffer();
    try {
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(buf);
        byte[] digest = algorithm.digest();
        for (int i = 0; i < digest.length; i++) {
            hexString.append(pad(Integer.toHexString(0xFF & digest[i]), 2));

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hexString.toString();
}