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 = null;//  ww w .java  2 s.co m
    String sTemp;

    if (bArray == null || bArray.length <= 0) {
        return null;
    }
    sb = new StringBuffer(bArray.length);
    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

/**
 * Transforms bytes to String/*from   ww w.j av  a 2s  .co  m*/
 *
 * @param data bytes
 * @return String
 */
public static String bytesToHex(byte[] data) {
    if (data == null)
        return null;

    String str = "";
    for (byte aData : data) {
        if ((aData & 0xFF) < 16)
            str = str + "0" + Integer.toHexString(aData & 0xFF);
        else
            str = str + Integer.toHexString(aData & 0xFF);
    }
    return str;
}

From source file:Main.java

public static String bytesToHexString(byte[] bArray) {
    if (bArray == null) {
        return null;
    }/*from  w w  w  . j  a v a2  s. c  o  m*/
    if (bArray.length == 0) {
        return "";
    }
    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 getCheckSum(String aString) {
    int checksum = 0;
    String checkedString = aString.substring(1, aString.indexOf("*"));

    for (int i = 0; i < checkedString.length(); i++) {
        checksum = checksum ^ checkedString.charAt(i);
    }/*from w  ww.j av a2s .  c  om*/

    String hex = Integer.toHexString(checksum);
    if (hex.length() == 1)
        hex = "0" + hex;

    return hex.toUpperCase();
}

From source file:Main.java

/**
 * Returns a string representation of the integer argument as an
 * unsigned integer in base&nbsp;16. The string will be uppercase
 * and will have a leading '0x'./*from ww w  . j  a  v  a 2 s  .c  o m*/
 *
 * @param value the integer value
 *
 * @return the hex string representation
 */
public static String toHexString(final int value) {
    return "0x" + Integer.toHexString(value).toUpperCase();
}

From source file:Main.java

public static String byteToHexString(byte b) {
    String ret = "";
    int intVal = b & 0xff;
    if (intVal < 0x10)
        ret += "0";
    ret += Integer.toHexString(intVal);
    return ret;/*from w  w  w  .  j a  v a2s  .  c om*/
}

From source file:Main.java

public static String byteArrayToHexString(byte[] b) {
    StringBuffer sb = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++) {
        int v = b[i] & 0xff;
        if (v < 16) {
            sb.append('0');
        }/*from www.  ja  va  2 s  .co m*/
        sb.append(Integer.toHexString(v));
    }
    return sb.toString().toUpperCase(Locale.US);
}

From source file:Main.java

public static String generateMd5Hash(String str) {
    try {/*from  www  . j  av a2  s. c  o m*/
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(str.getBytes("UTF8"));
        byte s[] = m.digest();
        String result = "";
        for (int i = 0; i < s.length; i++) {
            result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
        }
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String toHexString(byte[] bytes) {
    StringBuilder builder = new StringBuilder();
    for (byte b : bytes) {
        builder.append(Integer.toHexString((b >> 4) & 0xf));
        builder.append(Integer.toHexString(b & 0xf));
    }/*from  www .j  a  va 2s  . c  o m*/
    return builder.toString();
}

From source file:Main.java

public static String bytesToHexString(byte[] bytes) {
    StringBuilder stringBuilder = new StringBuilder("");
    if (bytes == null || bytes.length <= 0) {
        return null;
    }/*from  w ww .j a  v  a2 s . c  o  m*/
    for (int i = 0; i < bytes.length; i++) {
        int v = bytes[i] & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv).append(" ");
    }
    return stringBuilder.toString();
}