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 getRandomRGB() {
    String r, g, b;/*from   ww  w  . j  a  va 2s.c  o  m*/
    Random random = new Random();
    r = Integer.toHexString(random.nextInt(256)).toUpperCase();
    g = Integer.toHexString(random.nextInt(256)).toUpperCase();
    b = Integer.toHexString(random.nextInt(256)).toUpperCase();
    r = r.length() == 1 ? "0" + r : r;
    g = g.length() == 1 ? "0" + g : g;
    b = b.length() == 1 ? "0" + b : b;
    return "#" + (r + g + b);
}

From source file:Main.java

public static String getHexString(byte[] raw) {
    String str = new String();

    for (byte b : raw) {
        String hex = Integer.toHexString(b & 0xFF);

        if (hex.length() == 1) {
            str += "0" + hex;
        } else {//  w w  w. j a  v  a2  s . c  o m
            str += hex;
        }
    }

    return str;
}

From source file:Main.java

public static String arr2String(int[] arr) {
    String str = "";
    for (int i = 0; i < arr.length; i++) {
        String string = Integer.toHexString(arr[i]);
        if (string.length() == 1) {
            string = "-" + string;
        }//ww w. j a v a2 s  .com
        str = str + string;
    }
    return str;
}

From source file:Main.java

public static String getRandColorCode() {
    String r, g, b;/*  www  .ja  va2s.  c  o  m*/
    Random random = new Random();
    r = Integer.toHexString(random.nextInt(256)).toUpperCase();
    g = Integer.toHexString(random.nextInt(256)).toUpperCase();
    b = Integer.toHexString(random.nextInt(256)).toUpperCase();
    r = r.length() == 1 ? "0" + r : r;
    g = g.length() == 1 ? "0" + g : g;
    b = b.length() == 1 ? "0" + b : b;
    return r + g + b;
}

From source file:Main.java

public static String intToHexString(int a) {
    StringBuilder stringBuilder = new StringBuilder("");
    int v = a & 0xFF;
    String hv = Integer.toHexString(v);
    if (hv.length() < 2) {
        stringBuilder.append(0);//w  ww .  j a v  a2 s . com
    }
    stringBuilder.append(hv);
    return stringBuilder.toString();
}

From source file:Main.java

public static String convertTwoHexString(int num) {
    // return "0x" + Integer.toHexString(num).toUpperCase();

    String temp = Integer.toHexString(num).toUpperCase();
    switch (temp.length()) {
    case 1:/*from   w  w  w.j a  v a2 s.co  m*/
        return "0x000" + temp;
    case 2:
        return "0x00" + temp;
    case 3:
        return "0x0" + temp;
    default:
        return "0x" + temp;
    }
}

From source file:Main.java

public static String toHex(byte[] to) {
    if (null == to || to.length == 0)
        return "[]";
    StringBuffer sb = new StringBuffer("[" + Integer.toHexString(to[0] & 0xFF));
    for (int i = 1; i < to.length; i++) {
        sb.append(String.format(" %02x", to[i] & 0xFF));
    }// w  w  w.  j  a  v  a2 s  .  co  m
    sb.append("]");
    return sb.toString();
}

From source file:Main.java

public static String getPrintBytes(String tag, byte[] bytes) {
    String resp = "";
    for (int i = 0; i < bytes.length; i++) {
        resp += " " + Integer.toHexString(bytes[i]);
    }//from w w  w  . ja  v  a 2  s  .c  o m
    String msg = "Data:" + tag + " \t" + String.format("%03d", bytes.length) + ": " + resp;
    return msg;
}

From source file:Main.java

private static String toHexString(byte[] bytes) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
        sb.append(Integer.toHexString(bytes[i] >> 4 & 0x0f)).append(Integer.toHexString(bytes[i] & 0x0f));
    }/* w w w. ja v  a 2 s.co m*/
    return sb.toString();
}

From source file:Main.java

private static String toHexString(byte[] bytes) {
    StringBuilder hexString = new StringBuilder();
    for (byte b : bytes) {
        String str = Integer.toHexString(0xFF & b);
        while (str.length() < 2) {
            str = "0" + str;
        }/*from  w  w w  .j av  a2  s. c om*/
        hexString.append(str);
    }
    return hexString.toString();
}