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 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 {// w  w  w .j av  a  2s .  co  m
            hs = hs + stmp;
        }
    }
    return hs.toUpperCase();
}

From source file:Main.java

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/*  w ww . jav  a 2 s .co m*/
            hs = hs + stmp;
    }
    return hs.toUpperCase();
}

From source file:Main.java

public static String bytesToHexString(byte[] bytes) {
    String result = "";
    for (int i = 0; i < bytes.length; i++) {
        String hexString = Integer.toHexString(bytes[i] & 0xFF);
        if (hexString.length() == 1) {
            hexString = '0' + hexString;
        }/*from  w  w w .j  a  v a  2s . c  o  m*/
        result += hexString.toUpperCase();
    }
    return result;
}

From source file:Main.java

public static String intToHexString_4(int a) {
    StringBuilder stringBuilder = new StringBuilder("");
    int v = a & 0xFFFF;
    String hv = Integer.toHexString(v);
    if (hv.length() == 1) {
        stringBuilder.append("000");
    } else if (hv.length() == 2) {
        stringBuilder.append("00");
    } else {//from www .  j  a v  a  2 s  .c o  m
        stringBuilder.append("0");
    }
    stringBuilder.append(hv);
    return stringBuilder.toString();
}

From source file:Main.java

public static String byte2hexWithoutSpace(byte[] buffer) {
    String h = "";

    for (int i = 0; i < buffer.length; i++) {
        String temp = Integer.toHexString(buffer[i] & 0xFF);
        if (temp.length() == 1) {
            temp = "0" + temp;
        }//from   w w  w.  j a  va2s  . c o m
        h = h + temp;
    }

    return h;
}

From source file:Main.java

public static void printHexString(String hint, byte[] b) {
    System.out.print(hint);/* www.j a va  2 s .com*/
    for (int i = 0; i < b.length; i++) {
        String hex = Integer.toHexString(b[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        System.out.print(hex.toUpperCase() + " ");
    }
    System.out.println("");
}

From source file:Main.java

public static String bytes2Hex(byte[] bts) {
    String des = "";
    String tmp = null;/*  w  w  w.  jav a  2 s . c  o  m*/
    for (int i = 0; i < bts.length; i++) {
        tmp = (Integer.toHexString(bts[i] & 0xFF));
        if (tmp.length() == 1) {
            des += "0";
        }
        des += tmp;
    }
    return des;
}

From source file:Main.java

public static String toHex(byte[] buf) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
        String h = Integer.toHexString(0xFF & buf[i]);
        while (h.length() < 2)
            h = "0" + h;

        hexString.append(h);//  w  w w  .ja v a 2 s  .  c o m
    }

    return hexString.toString();
}

From source file:Main.java

public static String HexEncode(byte[] toencode) {
    StringBuilder sb = new StringBuilder(toencode.length * 2);
    for (byte b : toencode) {
        sb.append(Integer.toHexString((b & 0xf0) >>> 4));
        sb.append(Integer.toHexString(b & 0x0f));
    }/*from w  w w .  ja v  a  2 s. c  o  m*/
    return sb.toString();
}

From source file:Main.java

/****
 * //from www  .  j  a  va 2s.c om
 * @param deci
 * @return
 */
public static byte[] decimalToHex2Bytes(int deci) {
    String hexStr = Integer.toHexString(deci);
    if (hexStr.length() == 3) {
        hexStr = "0" + hexStr;
    } else if (hexStr.length() == 2) {
        hexStr = "00" + hexStr;
    } else if (hexStr.length() == 1) {
        hexStr = "000" + hexStr;
    }
    return hexStringToBytes(hexStr);
}