Java Utililty Methods Byte Array to Hex

List of utility methods to do Byte Array to Hex

Description

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

Method

StringbytesToHex(byte[] bytes)
bytes To Hex
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
for (int j = 0; j < len; j++) {
    buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
    buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
return buf.toString();
StringbytesToHex(byte[] bytes)
bytes To Hex
if (bytes == null) {
    return "";
String chars = "0123456789abcdef";
StringBuilder result = new StringBuilder(2 * bytes.length);
for (byte b : bytes) {
    int val = b & 0xff;
    result.append(chars.charAt(val / 16));
...
StringbytesToHex(byte[] bytes)
bytes To Hex
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
    int v = bytes[j] & 0xFF;
    hexChars[j * 2] = hexArray[v >>> 4];
    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
return new String(hexChars);
StringbytesToHex(byte[] bytes)
Convert an array of arbitrary bytes into a String of hexadecimal number-pairs with each pair representing on byte of the array.
final char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] hexChars = new char[bytes.length << 1];
for (int i = 0; i < bytes.length; i++) {
    int value = bytes[i] & 0xFF;
    int baseIndex = i << 1;
    hexChars[baseIndex] = hexArray[value >>> 4];
    hexChars[baseIndex + 1] = hexArray[value & 0x0F];
return new String(hexChars);
StringbytesToHex(byte[] bytes)
Creates a lowercase hexadecimal String representing a byte array.
char[] buffer = new char[2 * (bytes.length)];
int cursor = 0;
for (byte b : bytes) {
    int nibble1 = 0x0F & b >> 4;
    int nibble2 = 0x0F & b;
    buffer[cursor++] = (char) (nibble1 < 10 ? nibble1 + '0' : nibble1 - 10 + 'a');
    buffer[cursor++] = (char) (nibble2 < 10 ? nibble2 + '0' : nibble2 - 10 + 'a');
return new String(buffer);
StringbytesToHex(byte[] bytes)
This method converts an array of bytes to a hex string.
char[] hexChars = new char[bytes.length * 3];
for (int index = 0; index < bytes.length; index++) {
    int v = bytes[index] & 0xFF;
    hexChars[index * 3] = hexArray[v >>> 4];
    hexChars[index * 3 + 1] = hexArray[v & 0x0F];
    hexChars[index * 3 + 2] = ' ';
return new String(hexChars);
...
StringbytesToHex(byte[] bytes)
bytes To Hex
final char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
    int v = bytes[j] & 0xFF;
    hexChars[j * 2] = hexArray[v >>> 4];
    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
return new String(hexChars);
...
StringbytesToHex(byte[] bytes)
Bytes to hex.
StringBuffer result = new StringBuffer();
for (byte b : bytes)
    result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return result.toString();
voidbytesToHex(byte[] bytes, byte[] hex, int offset)
Turns 16-byte stream into a human-readable 32-byte hex string This code was copied from the PostgreSQL JDBC driver code (MD5Digest.java)
final char lookup[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
int i, c, j, pos = offset;
for (i = 0; i < 16; i++) {
    c = bytes[i] & 0xFF;
    j = c >> 4;
    hex[pos++] = (byte) lookup[j];
    j = (c & 0xF);
    hex[pos++] = (byte) lookup[j];
...
StringbytesToHex(byte[] bytes, int groupSize)
bytes To Hex
if (bytes == null) {
    return "null";
} else {
    char[] hexChars = new char[bytes.length * 2 + numberOfGroups(bytes, groupSize)];
    int outPos = 0;
    for (int j = 0; j < bytes.length; j++) {
        if (j > 0 && j % groupSize == 0) {
            hexChars[outPos++] = ' ';
...