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(byte[] bytes)
Converts an array of bytes to a string representing the bytes in hexadecimal form.
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
    String hex = Integer.toHexString(0x0100 + (b & 0x00FF)).substring(1);
    if (hex.length() < 2) {
        sb.append("0");
    sb.append(hex);
return sb.toString();
StringbytesToHexString(byte[] bytes)
bytes To Hex String
if (bytes == null) {
    return "";
StringBuffer buff = new StringBuffer();
int len = bytes.length;
for (int j = 0; j < len; j++) {
    if ((bytes[j] & 0xff) < 16) {
        buff.append('0');
...
StringbytesToHexString(byte[] bytes)
bytes To Hex String
if (bytes == null) {
    return null;
StringBuffer result = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
    result.append(Character.forDigit((bytes[i] & 0xF0) >> BITS_PER_NIBBLE, HEX_RADIX));
    result.append(Character.forDigit(bytes[i] & 0x0F, HEX_RADIX));
return result.toString();
StringbytesToHexString(byte[] bytes)
Converts a byte array to its 2-digit hexadecimal representation.
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
    sb.append(String.format("%1$2s", Integer.toHexString(b & 0xFF)).replaceAll(" ", "0").toUpperCase());
    sb.append(" ");
sb.delete(sb.length() - 1, sb.length());
return sb.toString();
StringbytesToHexString(byte[] bytes)
bytes To Hex String
return bytesToHexString(bytes, 0);
StringbytesToHexString(byte[] bytes)
bytes To Hex String
final char[] buf = new char[bytes.length * 2];
byte b;
int c = 0;
for (int i = 0, z = bytes.length; i < z; i++) {
    b = bytes[i];
    buf[c++] = DIGITS[(b >> 4) & 0xf];
    buf[c++] = DIGITS[b & 0xf];
return new String(buf);
StringbytesToHexString(byte[] bytes)
The fastest method to convert a byte array to hex string.
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++) {
    v = bytes[j] & 0xFF;
    hexChars[j * 2] = HEX_CHARS[v / 16];
    hexChars[j * 2 + 1] = HEX_CHARS[v % 16];
return new String(hexChars);
...
StringbytesToHexString(byte[] bytes)
bytes To Hex String
StringBuffer buf = new StringBuffer(bytes.length * 2);
for (byte b : bytes) {
    String s = Integer.toString(0xFF & b, 16);
    if (s.length() < 2)
        buf.append('0');
    buf.append(s);
return buf.toString();
...
StringbytesToHexString(byte[] data)
Converts a byte array to a string with hex values.
StringBuilder hexStrBuff = new StringBuilder(data.length * 2);
for (byte aData : data) {
    String hexByteStr = Integer.toHexString(aData & 0xff).toUpperCase();
    if (hexByteStr.length() == 1) {
        hexStrBuff.append("0");
    hexStrBuff.append(hexByteStr);
return hexStrBuff.toString();
StringbytesToHexString(byte[] data)
bytes To Hex String
StringBuilder valueHex = new StringBuilder();
for (int i = 0, tmp; i < data.length; i++) {
    tmp = data[i] & 0xff;
    if (tmp < 16) {
        valueHex.append(0);
    valueHex.append(Integer.toHexString(tmp));
return valueHex.toString();