Java Utililty Methods Byte Array to Hex String

List of utility methods to do Byte Array to Hex String

Description

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

Method

StringbytesToHexString(final byte[] bytes)
Convert a byte array to an hexadecimal string.
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
    int hex = (0xff & bytes[i]);
    String tmp = Integer.toHexString(hex);
    tmp = (tmp.length() < 2) ? "0" + tmp : tmp; 
    hexString.append(tmp);
return hexString.toString().toUpperCase();
...
StringbytesToHexString(final byte[] bytes)
Convert a byte array into a hex string.
final StringBuilder builder = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
    builder.append(table[b >> 4 & 0x0f]).append(table[b & 0x0f]);
return builder.toString();
StringbytesToHexString(final byte[] bytes)
bytes To Hex String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
    byte b = bytes[i];
    String hex = Integer.toHexString(b);
    if (hex.length() == 1) {
        hex = "0" + hex;
    if (hex.length() > 2) {
...
StringbytesToHexString(final byte[] bytes, int start, int end)
Returns the string representation of the given bytes .
if (bytes == null || bytes.length == 0) {
    return EMPTY_STRING;
final StringBuilder builder = new StringBuilder((end - start) << 1);
for (int index = start; index < end; index++) {
    builder.append(String.format("%02x", bytes[index]));
return builder.toString();
...
StringbytesToHexString(final byte[] data)
Convert a byte array into a String hex representation.
final char[] chars = new char[2 * data.length];
for (int i = 0; i < data.length; i++) {
    final byte b = data[i];
    chars[2 * i] = toHexDigit((b >> 4) & 0xF);
    chars[2 * i + 1] = toHexDigit(b & 0xF);
return new String(chars);
StringbytesToHexStringLine(byte[] bs, int lineLength)
bytes To Hex String Line
if (bs == null || bs.length == 0) {
    return "";
StringBuffer str = new StringBuffer(bs.length * 4);
for (int i = 0; i < bs.length; i++) {
    str.append(hex[(bs[i] >> 4) & 0x0f]);
    str.append(hex[bs[i] & 0x0f]);
    if (i > 0 && i % lineLength == lineLength - 1) {
...
StringbytesToHexStringWithSpace(byte[] bytes)
bytes To Hex String With Space
char[] hexChars = new char[2 + ((bytes.length - 1) * 3)];
for (int j = 0; j < bytes.length; j++) {
    int v = bytes[j] & 0xFF;
    hexChars[j * 3] = hexArray[v >>> 4];
    hexChars[j * 3 + 1] = hexArray[v & 0x0F];
    if (hexChars.length < (j * 3 + 2)) {
        hexChars[j * 3 + 2] = ' ';
return new String(hexChars);