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

/**
 *  Like DataHelper.toHexString but ensures no loss of leading zero bytes
 *  @since 0.8.4/*from w w  w. j av  a2s.  c  om*/
 */
public static String toHex(byte[] b) {
    StringBuilder buf = new StringBuilder(40);
    for (int i = 0; i < b.length; i++) {
        int bi = b[i] & 0xff;
        if (bi < 16)
            buf.append('0');
        buf.append(Integer.toHexString(bi));
    }
    return buf.toString();
}

From source file:Main.java

private static int[] getSnrAndChannelData(String snr, String channel) {
    List<Integer> dateItem = new ArrayList();
    int intSrn = Integer.valueOf(snr) * 100;
    String hexSnr = Integer.toHexString(intSrn);

    if (hexSnr.length() < 4) {
        switch (hexSnr.length()) {
        case 1:/*from ww  w.  ja  va2 s .c o m*/
            hexSnr = "000" + hexSnr;
            break;
        case 2:
            hexSnr = "00" + hexSnr;
            break;
        case 3:
            hexSnr = "0" + hexSnr;
            break;
        }
    }
    dateItem.add(Integer.valueOf(hexSnr.substring(2, 4), 16));
    dateItem.add(Integer.valueOf(hexSnr.substring(0, 2), 16));
    for (int i = 0; i < 6; i++) {
        dateItem.add(0);
    }
    String hexChannel = Integer.toHexString(Integer.valueOf(channel));
    if (hexChannel.length() < 4) {
        switch (hexChannel.length()) {
        case 1:
            hexChannel = "000" + hexChannel;
            break;
        case 2:
            hexChannel = "00" + hexChannel;
            break;
        case 3:
            hexChannel = "0" + hexChannel;
            break;
        }
    }
    dateItem.add(Integer.valueOf(hexChannel.substring(2, 4), 16));
    dateItem.add(Integer.valueOf(hexChannel.substring(0, 2), 16));
    for (int i = 0; i < 7; i++) {
        dateItem.add(0);
    }
    int[] message = new int[dateItem.size()];
    for (int i = 0; i < dateItem.size(); i++) {
        //            Logs.e("dateItem" + i + "===" + dateItem.get(i));
        message[i] = dateItem.get(i);
    }

    return message;

}

From source file:Main.java

/**
 * Get the hashcode for a bitmap./* w w w.j av  a2  s  .c  o  m*/
 */
private static String hashBitmap(Bitmap bmp) {
    int[] allpixels = new int[bmp.getHeight() * bmp.getWidth()];
    bmp.getPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
    return Integer.toHexString(Arrays.hashCode(allpixels));
}

From source file:Main.java

public static String toMD5(String md5) {
    try {/*  w  w w  . ja  v a2  s.  c om*/
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

/**
 * Convert byte array to hex string//from   w w  w  .  j a  v a2 s  . co  m
 * @param bytes
 * @return
 */
public static String bytesToHex(byte[] bytes) {
    StringBuilder sbuf = new StringBuilder();
    for (int idx = 0; idx < bytes.length; idx++) {
        int intVal = bytes[idx] & 0xff;
        if (intVal < 0x10)
            sbuf.append("0");
        sbuf.append(Integer.toHexString(intVal).toUpperCase());
    }
    return sbuf.toString();
}

From source file:Main.java

public static int[] getStepData(String step, String funCode) {
    List<Integer> dateItem = new ArrayList();
    dateItem.add(Integer.valueOf(funCode));
    int intStep = Integer.valueOf(step) * 100;
    String hexStep = Integer.toHexString(intStep);

    if (hexStep.length() < 4) {
        switch (hexStep.length()) {
        case 1:/*from   ww  w  . j a  v a2s.  c om*/
            hexStep = "000" + hexStep;
            break;
        case 2:
            hexStep = "00" + hexStep;
            break;
        case 3:
            hexStep = "0" + hexStep;
            break;
        }
    }
    dateItem.add(Integer.valueOf(hexStep.substring(2, 4), 16));
    dateItem.add(Integer.valueOf(hexStep.substring(0, 2), 16));
    for (int i = 0; i < 6; i++) {
        dateItem.add(0);
    }

    int[] message = new int[dateItem.size()];
    for (int i = 0; i < dateItem.size(); i++) {
        message[i] = dateItem.get(i);
    }

    return message;

}

From source file:Main.java

static String stringToHex(String str) {
    char[] chars = str.toCharArray();
    StringBuffer strBuffer = new StringBuffer();
    for (int i = 0; i < chars.length; i++) {
        strBuffer.append(Integer.toHexString((int) chars[i]));
    }//ww w .j a  v  a2  s  .c o m
    return strBuffer.toString();
}

From source file:Main.java

public static String byteArrayToHex(byte[] src, int from, int to) {

    StringBuilder sb = new StringBuilder();

    for (int i = from; i < to; i++) {
        int v = src[i] & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            sb.append(0);//from  w w  w .java 2  s. c o m
        }
        sb.append(hv);
    }
    return sb.toString();
}

From source file:Main.java

public static String getMd5String(String md5) {
    try {/*from  w  w w. ja  v a 2  s .  com*/
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Convert from int data into String hexadecimal (ex 255 => "0xFF")
 *
 * @param data data to convert into hexa
 * @return data converted into hexa/*from w ww.ja v a 2s  . c om*/
 */
public static String convertFromIntToHexa(byte data) {
    int dataTmp = data & 0xFF;
    /* Put character in uppercase */
    String value = Integer.toHexString(dataTmp).toUpperCase();
    /* Add 0 if length equal to 1 */
    if (value.length() == 1) {
        value = "0" + value;
    }
    return value;
}