/**
* Return a hex-dump formatted version of a byte array.
*/
public static String hexDump(byte[] data)
{
StringBuffer result = new StringBuffer();
StringBuffer line;
int pos = 0;
int max = data.length;
if (data.length == 0)
return "";
while (pos < max)
{
int lineMax = Math.min(max-pos, 16);
line = new StringBuffer();
// Left half of the hex dump (binary digits)
for (int i=0; i<lineMax; i++)
{
int b = data[pos+i];
b = (b + 256) % 256;
String code = Integer.toString(b, 16);
if (b <= 0x0F)
line.append('0');
line.append(code);
line.append(' ');
}
while (line.length() < 48)
line.append(' ');
// Right half of the hex dump (character data)
for (int i=0; i<lineMax; i++)
{
char c = (char)data[pos+i];
if ((c < 32) || (c > 127))
c = '.';
line.append(c);
}
line.append('\n');
result.append(line);
pos += 16;
}
return result.toString();
}