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

Stringbytes2HexStringWithSeparator(String separator, byte... bytes)
bytes Hex String With Separator
StringBuffer sb = new StringBuffer();
boolean first = true;
for (byte b : bytes) {
    if (first && b == 0x00) {
        continue;
    if (!first) {
        sb.append(separator);
...
StringbytesToHex(byte bytes[], int offset, int length, boolean wrap)
bytes To Hex
StringBuffer sb = new StringBuffer();
boolean newLine = false;
for (int i = offset; i < offset + length; ++i) {
    if (i > offset && !newLine) {
        sb.append(" ");
    sb.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
    if (i > 0 && (i + 1) % 16 == 0 && wrap) {
...
StringbytesToHex(byte in[])
bytes To Hex
byte ch = 0x00;
int i = 0;
if (in == null || in.length <= 0)
    return null;
String pseudo[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
StringBuffer out = new StringBuffer(in.length * 2);
while (i < in.length) {
    ch = (byte) (in[i] & 0xF0); 
...
StringbytesToHex(byte... bytes)
Convert bytes to HEX values in a string ("null safe").
if (bytes == null) {
    return null;
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 * 2];
for (int i = 0; i < bytes.length; i++) {
    hexChars[i * 2] = hexArray[(Byte.toUnsignedInt(bytes[i]) >>> 4)];
    hexChars[i * 2 + 1] = hexArray[bytes[i] & 0x0F];
...
StringbytesToHex(byte[] a)
bytes To Hex
if (a == null || a.length == 0) {
    return null;
StringBuffer buf = new StringBuffer(a.length * 2);
int hi, lo;
for (int i = 0; i < a.length; i++) {
    lo = (a[i]) & 0xff;
    hi = (lo >> 4);
...
StringbytesToHex(byte[] a)
bytes To Hex
return bytesToHex(a, a.length);
StringbytesToHex(byte[] array)
Convenience method to convert a byte array to a hex string.
char[] val = new char[2 * array.length];
String hex = "0123456789ABCDEF";
for (int i = 0; i < array.length; i++) {
    int b = array[i] & 0xff;
    val[2 * i] = hex.charAt(b >>> 4);
    val[2 * i + 1] = hex.charAt(b & 15);
return String.valueOf(val);
...
StringbytesToHex(byte[] b)
Converts a byte array to a representative string of hex digits.
String s = "";
for (int i = 0; i < b.length; i++) {
    if (i > 0 && i % 4 == 0) {
        s += " ";
    s += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
return s;
...
StringbytesToHex(byte[] b)
bytes To Hex
String result = "";
for (int i = 0; i < b.length; i++) {
    result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
return result;
StringbytesToHex(byte[] b)
bytes To Hex
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
StringBuffer buf = new StringBuffer();
for (int j = 0; j < b.length; j++) {
    buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
    buf.append(hexDigit[b[j] & 0x0f]);
return buf.toString();