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

private static String byteToHexStr(byte[] input) {
    if (input == null) {
        return "";
    }/*from w  w w  . j a  v  a 2  s.  c  o  m*/
    String output = "";
    String tmp = "";
    for (int n = 0; n < input.length; n++) {
        tmp = Integer.toHexString(input[n] & 0xFF);
        if (tmp.length() == 1) {
            output = output + "0" + tmp;
        } else {
            output = output + tmp;
        }
    }
    return output.toUpperCase(Locale.ENGLISH);
}

From source file:Main.java

public static String toHex(byte abyte0[]) {
    StringBuilder stringbuilder = new StringBuilder(2 * abyte0.length);
    int i = 0;//from   w  w  w . j  av  a2  s  .  c o m
    while (i < abyte0.length) {
        String s = Integer.toHexString(abyte0[i]);
        if (s.length() == 1) {
            stringbuilder.append("0");
        } else if (s.length() == 8) {
            s = s.substring(6);
        }
        stringbuilder.append(s);
        i++;
    }
    return stringbuilder.toString().toLowerCase(Locale.getDefault());
}

From source file:Main.java

public static String get_recv_data_str(byte[] rcv_buff, int len) {
    int i;//from   ww w  .  ja  va  2  s  .  c o m

    StringBuffer sb = new StringBuffer();
    for (i = 0; i < len; i++) {
        if ((0x00f0 & ((char) rcv_buff[i])) == 0)
            sb.append(0);
        sb.append(Integer.toHexString(0x00ff & ((char) rcv_buff[i])).toUpperCase());
        //            if((i+1)%16 == 0) sb.append("\r\n");
    }
    String s = sb.toString();
    return s;
}

From source file:Main.java

public static StringBuilder toHexString(byte[] src) {
    if (src == null || src.length <= 0) {
        return null;
    }//www  . j  av  a2 s.c o  m

    StringBuilder stringBuilder = new StringBuilder("");
    for (int i = 0; i < src.length; i++) {
        String hv = Integer.toHexString(src[i] & 0xFF);
        if (hv.length() == 1) {
            stringBuilder.append(0);
        }
        stringBuilder.append(hv);
    }
    return stringBuilder;
}

From source file:Main.java

public static String bytesToHex(byte[] data) {
    if (data == null) {
        return null;
    }//from   w  w w  . ja va 2 s .co  m
    int len = data.length;
    String str = "";
    for (int i = 0; i < len; i++) {
        if ((data[i] & 0xFF) < 16)
            str = str + "0" + Integer.toHexString(data[i] & 0xFF);
        else
            str = str + Integer.toHexString(data[i] & 0xFF);
    }
    return str;
}

From source file:Main.java

/**
 * @param context// www . j  av a 2  s  .c  o  m
 * @param res
 * @return
 */
public static String color2hex(Context context, int res) {
    int color = context.getResources().getColor(res);
    String s = Integer.toHexString(color);
    if (s.length() == 8) {
        s = s.substring(2);
    }
    return s;
}

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String byte2hex(byte[] b) {
    String hs = "";
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
        stmp = (Integer.toHexString(b[n] & 0XFF));
        if (stmp.length() == 1) {
            hs = hs + "0" + stmp;
        } else {/*from www  . jav a  2 s.co  m*/
            hs = hs + stmp;
        }
    }
    return hs.toUpperCase();
}

From source file:Main.java

public static String hexString(byte[] bytes) {
    StringBuffer hexValue = new StringBuffer();

    for (int i = 0; i < bytes.length; i++) {
        int val = ((int) bytes[i]) & 0xff;
        if (val < 16)
            hexValue.append("0");
        hexValue.append(Integer.toHexString(val));
    }//  w  w w . ja va2  s  .  co  m
    return hexValue.toString();
}

From source file:Main.java

/**
 * Transform integer RBG to hex color string
 *///from  w  w  w .  j ava 2 s  .  c o m
public static String rgbToHex(int[] color) {
    String colorString = "#";
    for (int element : color) {
        if (element < 16) {
            colorString += "0";
        }
        colorString += Integer.toHexString(element);
    }
    return colorString;
}

From source file:Main.java

public static String toHex(byte[] data) {
    String ret = null;//from  w  w w.  j ava  2  s.c  o  m

    if (data != null && data.length > 0) {
        StringBuilder sb = new StringBuilder();
        for (byte b : data) {

            int v = b & 0x0FF;
            String hexString = Integer.toHexString(v);
            if (v > 0x0F) {
                sb.append(hexString);
            } else {
                sb.append('0').append(hexString);
            }
        }
        ret = sb.toString();
    }

    return ret;
}