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 final String bytesToHexString(byte[] bArray) {
    StringBuffer sb = new StringBuffer(bArray.length);

    for (int i = 0; i < bArray.length; i++) {
        String sTemp = Integer.toHexString(0xFF & bArray[i]);
        if (sTemp.length() < 2) {
            sb.append(0);//from  w  ww.j  a va  2 s.c om
        }
        sb.append(sTemp.toUpperCase(Locale.US));
    }
    return sb.toString();
}

From source file:Main.java

private static String getMD5(byte[] source) {
    try {/*  w w  w  .j  a v  a2  s.  c o m*/
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        StringBuffer result = new StringBuffer();
        for (byte b : md5.digest(source)) {
            result.append(Integer.toHexString((b & 0xf0) >>> 4));
            result.append(Integer.toHexString(b & 0x0f));
        }
        return result.toString();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Main.java

/**
 * Gets a hex string from byte array.//from ww w  .  ja v a2  s  . c om
 * 
 * @param res
 *            the byte array
 * @return the hex string representing the binary values in the array
 */
public static final String toHexString(byte[] res) {
    StringBuffer buf = new StringBuffer(res.length << 1);

    for (int ii = 0; ii < res.length; ii++) {
        String digit = Integer.toHexString(0xFF & res[ii]);

        if (digit.length() == 1) {
            digit = '0' + digit;
        }

        buf.append(digit);
    }
    return buf.toString().toUpperCase();
}

From source file:Main.java

public static String paddedByteString(byte b) {
    int extended = (b | 0x100) & 0x1ff;
    return Integer.toHexString(extended).toUpperCase().substring(1);
}

From source file:HTML.java

/**
 * Utility method to format a color to HTML RGB color format (e.g. #FF0000 for Color.red).
 * @param color The color.//from ww  w .  j a v a  2  s.c o  m
 * @return the HTML RGB color string.
 */
public static final String format(Color c) {
    String r = (c.getRed() < 16) ? "0" + Integer.toHexString(c.getRed()) : Integer.toHexString(c.getRed());
    String g = (c.getGreen() < 16) ? "0" + Integer.toHexString(c.getGreen())
            : Integer.toHexString(c.getGreen());
    String b = (c.getBlue() < 16) ? "0" + Integer.toHexString(c.getBlue()) : Integer.toHexString(c.getBlue());
    return "#" + r + g + b;
}

From source file:SHA1Sum.java

private static String hexChr(int b) {
    return Integer.toHexString(b & 0xF);
}

From source file:jp.co.nemuzuka.utils.BinaryHexConverter.java

/**
 * byte??16???/*from w w  w  .  j a  va2  s  .c om*/
 * @param fromByte ?Byte?
 * @return 16???(?byte??null???null)
 */
public static String bytesToHexString(byte[] fromByte) {

    if (fromByte == null) {
        return null;
    }

    StringBuilder hexStrBuilder = new StringBuilder();

    for (int i = 0; i < fromByte.length; i++) {
        // 16?1??????2?0??
        if ((fromByte[i] & 0xff) < 0x10) {
            hexStrBuilder.append("0");
        }
        hexStrBuilder.append(Integer.toHexString(0xff & fromByte[i]).toUpperCase());
    }
    return hexStrBuilder.toString();
}

From source file:fm.last.util.StringUtils.java

/**
 * Escapes the unicode in the given string.
 * /*  w w w  .ja  va2s  .  c  o  m*/
 * @param input The input string.
 * @param escapeAscii Whether to escape ASCII or not.
 * @return The escaped string.
 */
public static String escapeUnicodeString(final String input, final boolean escapeAscii) {
    StringBuffer returnValue = new StringBuffer("");
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if (!escapeAscii && ((ch >= 0x0020) && (ch <= 0x007e))) {
            returnValue.append(ch);
        } else {
            returnValue.append("\\u");

            String hex = Integer.toHexString(input.charAt(i) & 0xFFFF);
            if (hex.length() == 2) {
                returnValue.append("00");
            }
            returnValue.append(hex.toUpperCase());
        }
    }
    return returnValue.toString();
}

From source file:Main.java

public static String byte2Hex(byte[] b) {
    StringBuilder hs = new StringBuilder(b.length);
    String stmp = "";
    int len = b.length;
    for (int n = 0; n < len; n++) {
        stmp = Integer.toHexString(b[n] & 0xFF);
        if (stmp.length() == 1)
            hs = hs.append("0").append(stmp);
        else/*from w ww  .j av  a  2 s .co  m*/
            hs = hs.append(stmp);
    }
    return String.valueOf(hs);
}

From source file:MessageDigestUtil.java

/**
 * byte[]?16??????????/* w  w w  . j  av a2  s  .  c o  m*/
 * 
 * @param buf
 * @return
 */
public static String toHexString(byte[] buf) {
    String digestText = "";
    for (int i = 0; i < buf.length; i++) {
        int n = buf[i] & 0xff;
        if (n < 16) {
            digestText += "0";
        }
        digestText += Integer.toHexString(n).toUpperCase();
    }
    return digestText;
}