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

Stringbytes2hexStr(byte[] arr, int len)
Function: bytes2hexStr (byte [] arr, int len)( Generate a hex string showing the value of the bytes in ARR.
StringBuffer sb = new StringBuffer(len * 2);
for (int i = 0; i < len; i++) {
    int hi = (arr[i] >>> 4) & 0xf;
    sb.append(hex[hi]);
    int low = (arr[i]) & 0xf;
    sb.append(hex[low]);
return sb.toString();
...
Stringbytes_to_hex(byte[] b)
bytethex
String result = "";
for (int i = 0; i < b.length; i++) {
    result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
return result;
Stringbytes_to_hex(byte[] bytes)
bytethex
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);
voidbytesToHexAppend(byte[] bs, int off, int length, StringBuffer sb)
bytes To Hex Append
sb.ensureCapacity(sb.length() + length * 2);
for (int i = off; i < (off + length) && i < bs.length; i++) {
    sb.append(Character.forDigit((bs[i] >>> 4) & 0xf, 16));
    sb.append(Character.forDigit(bs[i] & 0xf, 16));
char[]bytesToHexChars(byte[] bytes)
bytes To Hex Chars
return bytesToHexChars(bytes, new char[bytes.length * 2], 0);
char[]bytesToHexChars(byte[] bytes)
Transforms bytes to an array of hex characters representing the nibbles
char[] chars = new char[bytes.length * 2];
int charPos = chars.length - 1;
for (int bytePos = bytes.length - 1; bytePos >= 0; bytePos--) {
    chars[charPos--] = HEX.charAt(bytes[bytePos] & 0x0f);
    chars[charPos--] = HEX.charAt((bytes[bytePos] & 0xf0) >> 4);
return chars;
char[]bytesToHexChars(byte[] bytes)

Convert binary data into a sequence of pairs of hexadecimal character values.

if (bytes == null)
    throw new NullPointerException("Cannot convert a null byte array to hex string.");
char[] chars = new char[bytes.length * 2];
int charCounter = 0;
for (int x = 0; x < bytes.length; x++) {
    chars[charCounter++] = hexChars[(bytes[x] >>> 4) & 0x0f];
    chars[charCounter++] = hexChars[bytes[x] & 0x0f];
return chars;
StringbytesToHexChecksum(byte[] byteArr)
bytes To Hex Checksum
StringBuilder buf = new StringBuilder();
for (int i = 0; i < byteArr.length; i++) {
    buf.append(Integer.toHexString(0xFF & byteArr[i]).toUpperCase());
return buf.toString();
StringbytesToHexDelimeter(byte[] data, String delimeter)
Convenience method to convert a byte array to a hex string and seperate with delimeter.
if (data == null)
    return "null";
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
    buf.append(byteToHex(data[i]).toUpperCase());
    if (i != data.length - 1) {
        buf.append(delimeter);
return (buf.toString());
StringbytesToHexFormatted(byte[] bytes)
bytes To Hex Formatted
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);