Java Utililty Methods Byte to Hex

List of utility methods to do Byte to Hex

Description

The list of methods to do Byte to Hex are organized into topic(s).

Method

StringbytesToPrettyHex(byte[] data)
bytes To Pretty Hex
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < data.length; i++) {
    buffer.append(String.format("%02x ", data[i]));
    if (((i + 1) % 16) == 0) {
        buffer.append("| ");
        for (int j = i - 15; j <= i; j++)
            buffer.append(getPrintableChar((char) data[j]));
        buffer.append(" |\n");
...
StringbytesToReadableHexStr(byte[] msg)
Convert a byte array to a formated hex string suitable for printing.
return bytesToReadableHexStr(msg, 0, msg.length);
StringbytesToUpperCaseHex(byte[] b)
bytes To Upper Case Hex
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
StringBuffer buf = new StringBuffer();
for (int j = 0; j < b.length; j++) {
    buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
    buf.append(hexDigit[b[j] & 0x0f]);
return buf.toString();
StringbyteToHex(byte a)
byte To Hex
StringBuffer s = new StringBuffer();
s.append(Character.forDigit((a >> 4) & 0xf, 16));
s.append(Character.forDigit(a & 0xf, 16));
return s.toString();
StringbyteToHex(byte b)
byte To Hex
int high = (b & 0xF0) >> 4;
int low = (b & 0x0F);
return "" + HEX.charAt(high) + HEX.charAt(low);
StringbyteToHex(byte b)
Converts a byte to the string representation of its hex value.
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
return new String(array);
StringbyteToHex(byte b)
byte To Hex
return String.format("0x%02X", b);
StringbyteToHex(byte b)
Converts the given byte to a hex String.
String hex = Integer.toHexString(b);
if (hex.length() >= 2) {
    return hex.substring(hex.length() - 2);
return String.format("0%s", hex);
StringbyteToHex(byte b)
Returns the hexadecimal representation of a byte.
return String.format("%02X", b);
StringbyteToHex(byte b)
byte To Hex
return hexNums[b >> 4 & 0x0f] + hexNums[b & 0x0f];