Java Byte Array Dump dumphex(byte[] bytes)

Here you can find the source of dumphex(byte[] bytes)

Description

dumphex

License

Apache License

Declaration

public static String dumphex(byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static String dumphex(byte[] bytes) {
        int bsLen = bytes.length;
        String head = "-Location- -0--1--2--3--4--5--6--7--8--9--A--B--C--D--E--F--0--1--2--3--4--5--6--7--8--9--A--B--C--D--E--F- ---ASCII Code---\n";
        StringBuilder ret = new StringBuilder(head.length() + bsLen * 3);
        ret.append(head);//w  w w .  j av  a2  s  .  c om
        for (int i = 0; i < bsLen;) {
            ret.append(lpadding(Integer.toHexString(i), 4, "0"))
                    .append('(');
            ret.append(lpadding("" + i, 4, "0")).append(") ");
            for (int j = 0; j < 32; j++) {
                String hex = i + j >= bsLen ? ".." : Integer
                        .toHexString((int) (bytes[i + j] & 0xff));
                if (hex.length() < 2)
                    ret.append("0");
                ret.append(hex).append(' ');
            }
            ret.append(' ');
            for (int j = 0; j < 32; j++) {
                if (i + j >= bsLen)
                    ret.append('.');
                else if (bytes[i + j] < 20 && bytes[i + j] >= 0)
                    ret.append('*');
                else {
                    if (bytes[i + j] > 0)
                        ret.append((char) bytes[i + j]);
                    else if (bsLen > i + j + 1) {
                        String s = new String(bytes, i + j, 2);
                        ret.append(s);
                        j++;
                    } else
                        ret.append((char) bytes[i + j]);
                }
            }
            ret.append('\n');
            i += 32;
        }
        return ret.toString();
    }

    private static String lpadding(String s, int n, String padding) {
        StringBuilder strbuf = new StringBuilder();
        for (int i = 0; i < n - s.length(); i++) {
            strbuf.append(padding);
        }
        strbuf.append(s);
        return strbuf.toString();
    }
}

Related

  1. dumpBytes(byte[] data)
  2. dumpBytes(final byte[] bytes)
  3. dumpBytes(String headerStr, byte[] bytes)
  4. dumpBytesAsInt(byte[] b)
  5. dumpCodeBytes(byte[] data)
  6. dumpHexBytes(byte[] bytes)
  7. dumpHexData(String title, byte[] buf, int numBytes)