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 writeArrayAsHex(byte[] array, boolean packedPrint) {
    if (packedPrint) {
        return convertToHex(array);
    }//from w  w  w.  j  a  v  a  2  s . c o  m

    StringBuilder builder = new StringBuilder();
    for (byte b : array) {
        builder.append(" 0x");
        String hex = Integer.toHexString(b);
        switch (hex.length()) {
        case 1:
            builder.append('0').append(hex);
            break;
        case 2:
            builder.append(hex);
            break;
        default:
            builder.append(hex.substring(6, 8));
        }
    }

    return builder.toString();
}

From source file:com.bstore.services.test.TestMD5.java

private static String generateToken() {
    MessageDigest md;//from ww w  .  j av  a  2  s  . c o  m
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("com.bstore.services.test.TestMD5.generateToken():" + e);
        throw new RuntimeException(e);
    }

    StringBuffer hexString = new StringBuffer();
    byte[] data = md.digest(RandomStringUtils.randomAlphabetic(10).getBytes());
    for (int i = 0; i < data.length; i++) {
        hexString.append(Integer.toHexString((data[i] >> 4) & 0x0F));
        hexString.append(Integer.toHexString(data[i] & 0x0F));
    }
    System.out.println("com.bstore.services.test.TestMD5.generateToken():: " + hexString.toString());
    return hexString.toString();
}

From source file:Main.java

/**
 * This method converts a decimal representation of an value into the corresponding
 * hex representation.//  www.j  ava  2 s . com
 * 
 * @param decimalValue the decimal presentation of anything.
 * @return a String containing the hex presentation of anything.
 */
public static String toHexOutput(int decimalValue) {
    return Integer.toHexString(decimalValue);
}

From source file:Main.java

/**
 * Returns the hexadecimal value of the supplied byte array. The resulting string always uses two
 * hexadecimals per byte. As a result, the length of the resulting string is guaranteed to be twice the
 * length of the supplied byte array./*  w ww . ja  v  a  2s.c  o  m*/
 */
public static String toHexString(byte[] array) {
    StringBuilder sb = new StringBuilder(2 * array.length);

    for (int i = 0; i < array.length; i++) {
        String hex = Integer.toHexString(array[i] & 0xff);

        if (hex.length() == 1) {
            sb.append('0');
        }

        sb.append(hex);
    }

    return sb.toString();
}

From source file:Main.java

public static String computeMD5(byte[] input) {
    try {//www.ja  v  a2  s .  c o  m
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input, TYPE_NOT_CONNECTED, input.length);
        byte[] md5bytes = md.digest();
        StringBuffer hexString = new StringBuffer();
        for (int i = TYPE_NOT_CONNECTED; i < md5bytes.length; i += TYPE_WIFI) {
            String hex = Integer.toHexString(md5bytes[i] & MotionEventCompat.ACTION_MASK);
            if (hex.length() == TYPE_WIFI) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:MD5.java

public static String create(String content) throws MD5IsNotSupported {
    String result = "";
    try {/*w w w  . j a  va2 s.  c  om*/
        byte[] defaultBytes = content.getBytes();
        MessageDigest algorithm = MessageDigest.getInstance("MD5");
        algorithm.reset();
        algorithm.update(defaultBytes);
        byte messageDigest[] = algorithm.digest();

        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        }
        result = hexString.toString();
    } catch (NoSuchAlgorithmException ex) {
        throw new MD5IsNotSupported(ex);
    }

    assert !result.isEmpty();
    return result;
}

From source file:Main.java

/**
 * @return/*w  w  w .  j  a  va2s.  co m*/
 */
public static final String bytesToHexString(byte[] bArray) {
    StringBuffer sb = new StringBuffer(bArray.length);
    String sTemp;
    for (int i = 0; i < bArray.length; i++) {
        sTemp = Integer.toHexString(0xFF & bArray[i]);
        if (sTemp.length() < 2)
            sb.append(0);
        sb.append(sTemp.toUpperCase());
    }
    return sb.toString();
}

From source file:Main.java

private static String toHexadecimalString(byte[] value) {
    StringBuffer sb = new StringBuffer();
    int len = value.length;
    for (int i = 0; i < len; i++) {
        int num = ((int) value[i]) & 0xff;
        if (num < 0x10) {
            sb.append('0');
        }/*w w  w .j  av  a 2s .  co  m*/
        sb.append(Integer.toHexString(num));
        if (i < len - 1) {
            sb.append(':');
        }
    }
    return sb.toString().toUpperCase(Locale.US);
}

From source file:Main.java

public static String generate(String s) {
    try {//from   w  w w  . j a v  a2s .c o m
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] digest = messageDigest.digest(s.getBytes());
        StringBuilder md5 = new StringBuilder();
        for (byte value : digest) {
            md5.append(Integer.toHexString((value & 0xFF) | 0x100).substring(1, 3));
        }
        return md5.toString();
    } catch (NoSuchAlgorithmException e) {
        return null;
    }
}

From source file:Main.java

/**
 * Encode XML entities and unicode control chars
 * //from   w w  w. j  ava2s . c om
 * @param s
 *            the string to encode
 * @return the encoded string
 */
public static String encode(String s) {

    StringBuffer ret = new StringBuffer();

    for (int i = 0; i < s.length(); i++) {
        char ch = s.charAt(i);
        if (ch == '<') {
            ret.append("&lt;"); //$NON-NLS-1$
        } else if (ch == '>') {
            ret.append("&gt;"); //$NON-NLS-1$
        } else if (ch == '"') {
            ret.append("&quot;"); //$NON-NLS-1$
        } else if (ch == '\'') {
            ret.append("&apos;"); //$NON-NLS-1$
        } else if (ch == '&') {
            ret.append("&amp;"); //$NON-NLS-1$
        } else if (ch < 0x20 && ch != 0x9 && ch != 0xD && ch != 0xA) {
            ret.append((ch <= 0xf ? "\\u000" : "\\u00") //$NON-NLS-1$ //$NON-NLS-2$
                    + Integer.toHexString(ch).toUpperCase());
        } else {
            ret.append(ch);
        }
    }

    return ret.toString();
}