Java Byte Array Dump dumpBytes(String headerStr, byte[] bytes)

Here you can find the source of dumpBytes(String headerStr, byte[] bytes)

Description

Dumps (prints) the byte array.

License

Open Source License

Parameter

Parameter Description
headerStr String that will be printed as header
bytes Bytes to be dumped as hex.

Declaration

public static void dumpBytes(String headerStr, byte[] bytes) 

Method Source Code

//package com.java2s;

public class Main {
    final static char[] HEX_DIGIT = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**/* w  w w  . j  av a  2 s .c  om*/
     * Dumps (prints) the byte array. Debug method.
     * @param headerStr String that will be printed as header
     * @param bytes Bytes to be dumped as hex.
     */
    public static void dumpBytes(String headerStr, byte[] bytes) {
        StringBuffer buf = new StringBuffer(bytes.length);
        buf.append("\n");
        buf.append(headerStr).append("\n");
        buf.append("bytes.length: ").append(bytes.length).append("\n");
        int len = bytes.length;
        int i = 0;
        for (i = 0; i < len; i++) {
            buf.append(toHex(bytes[i]) + " ");
            if (0 == ((i + 1) % 8)) {
                buf.append("\n");
            }
        }
        buf.append("\n");
        System.out.println(buf.toString());
    }

    /**
     * Converts one int to a hexadecimal ASCII string.
     *
     * @param val The integer
     * @return The hex string
     */
    public static String toHex(int val) {
        int val1, val2;

        val1 = (val >> 4) & 0x0F;
        val2 = (val & 0x0F);

        return ("" + HEX_DIGIT[val1] + HEX_DIGIT[val2]);
    }
}

Related

  1. dumpBytes(byte[] bytes, int start, int length)
  2. dumpBytes(byte[] byts, int offset, int length)
  3. dumpBytes(byte[] data)
  4. dumpBytes(byte[] data)
  5. dumpBytes(final byte[] bytes)
  6. dumpBytesAsInt(byte[] b)
  7. dumpCodeBytes(byte[] data)
  8. dumphex(byte[] bytes)
  9. dumpHexBytes(byte[] bytes)