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

Stringbytes2Hex(byte[] byteArray)
bytes Hex
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
    if (byteArray[i] >= 0 && byteArray[i] < 16) {
        strBuf.append("0");
    strBuf.append(Integer.toHexString(byteArray[i] & 0xFF));
return strBuf.toString();
...
Stringbytes2Hex(byte[] bytes)
bytes Hex
StringBuilder stringBuilder = new StringBuilder("");
if (bytes == null || bytes.length <= 0) {
    return null;
for (int i = 0; i < bytes.length; i++) {
    int v = bytes[i] & 0xFF;
    String hv = Integer.toHexString(v);
    if (hv.length() < 2) {
...
Stringbytes2Hex(byte[] bytes)
bytes Hex
if (bytes == null) {
    return null;
final StringBuilder hexBuilder = new StringBuilder(2 * bytes.length);
for (final byte b : bytes) {
    hexBuilder.append(HEX_CHARS[(b & 0xF0) >> 4]).append(HEX_CHARS[b & 0x0F]);
return hexBuilder.toString();
...
Stringbytes2Hex(byte[] bytes)
bytes Hex
final StringBuilder result = new StringBuilder(2 * bytes.length);
for (final byte item : bytes) {
    result.append((char) HEX_ALPHABET[(item & 0xff) >> 4]).append((char) HEX_ALPHABET[item & 0xf]);
return result.toString();
Stringbytes2Hex(byte[] bytes)
bytes Hex
StringBuffer ret = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
    ret.append(byte2Hex(bytes[i]));
return ret.toString();
byte[]bytes2HexCharArr(byte[] bytes)
bytes Hex Char Arr
if (bytes == null)
    return null;
byte[] sb = new byte[bytes.length * 2];
copy2Hex(bytes, sb, 0);
return sb;
Stringbytes2HexPP2(byte[] bytes)
bytes Hex PP
char[] hexChars = new char[bytes.length * 3];
for (int j = 0; j < bytes.length; j++) {
    int v = bytes[j] & 0xFF;
    hexChars[j * 3] = ' ';
    hexChars[j * 3 + 1] = hexArray[v >>> 4];
    hexChars[j * 3 + 2] = hexArray[v & 0x0F];
return new String(hexChars).substring(1);
...
Stringbytes2HexSplit(byte[] in, int wordlength)
From Van Bui - prints out a hex string formatted with spaces between each hex word of length wordlength.
String hex = bytes2Hex(in);
StringBuffer buff = new StringBuffer();
for (int i = 0; i < hex.length(); i++) {
    buff.append(hex.charAt(i));
    if ((i + 1) % wordlength == 0)
        buff.append(" ");
return buff.toString();
...
Stringbytes2HexStr(byte[] bytes)
bytes Hex Str
return bytes2HexStr(bytes, false);
Stringbytes2HexStr(byte[] bytes)
bytes Hex Str
if ((bytes == null) || (bytes.length == 0)) {
    return null;
char[] chars = new char[bytes.length * 2];
for (int i = 0; i < bytes.length; i++) {
    byte b = bytes[i];
    chars[(i * 2 + 1)] = DIGITS[(b & 0xF)];
    b = (byte) (b >>> 4);
...