Java Byte Array Dump dumpHexData(String title, byte[] buf, int numBytes)

Here you can find the source of dumpHexData(String title, byte[] buf, int numBytes)

Description

This method dumps a byte array as hex characters.

License

Open Source License

Parameter

Parameter Description
title The title.
buf The byte array buffer.
numBytes The number of bytes to dump.

Declaration

public static String dumpHexData(String title, byte[] buf, int numBytes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final int BYTES_PER_ROW = 16;
    private static final int ROW_QTR1 = 3;
    private static final int ROW_HALF = 7;
    private static final int ROW_QTR2 = 11;
    private static final String LINE_SEPARATOR = System.lineSeparator();
    private static String[] hexChars = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B",
            "C", "D", "E", "F" };

    /**//from  w w  w  .  ja  va  2  s .  co m
     * This method dumps a byte array as hex characters.
     * @param title The title.
     * @param buf The byte array buffer.
     * @param numBytes The number of bytes to dump.
     * @param return A string containing the dumped bytes.
     */
    public static String dumpHexData(String title, byte[] buf, int numBytes) {
        int rows;
        int residue;
        int i;
        int j;
        int labelSize;
        byte[] rowBuf = new byte[BYTES_PER_ROW + 2];
        String[] charBuf = new String[4];
        String result = title + " - " + numBytes + " bytes." + LINE_SEPARATOR;

        // There are 16 bytes per row.
        rows = numBytes >> 4;
        residue = numBytes & 0x0000000F;

        /*
         * Calculate the number of hex characters required to represent the row
         * label.
         */
        labelSize = log16(numBytes);

        for (i = 0; i < rows; i++) {
            int byteCount = (i * BYTES_PER_ROW);
            String rowLabel = buildRowLabel(byteCount, labelSize);

            result = result.concat(rowLabel + ": ");

            for (j = 0; j < BYTES_PER_ROW; j++) {
                rowBuf[j] = buf[(i * BYTES_PER_ROW) + j];

                charBuf[0] = hexChars[(rowBuf[j] >> 4) & 0x0F];
                charBuf[1] = hexChars[rowBuf[j] & 0x0F];

                result = result.concat(charBuf[0]);
                result = result.concat(charBuf[1]);
                result = result.concat(" ");

                if (j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2) {
                    result = result.concat(" ");
                }

                if (rowBuf[j] < 0x20 || rowBuf[j] > 0xD9) {
                    rowBuf[j] = '.';
                }
            }

            String saveStr = new String(rowBuf, 0, j);
            result = result.concat(" | " + saveStr + " |" + LINE_SEPARATOR);
        }

        if (residue > 0) {
            int byteCount = (i * BYTES_PER_ROW);
            String rowLabel = buildRowLabel(byteCount, labelSize);
            result = result.concat(rowLabel + ": ");

            for (j = 0; j < residue; j++) {
                rowBuf[j] = buf[(i * BYTES_PER_ROW) + j];

                charBuf[0] = hexChars[(rowBuf[j] >> 4) & 0x0F];
                charBuf[1] = hexChars[rowBuf[j] & 0x0F];

                result = result.concat(charBuf[0]);
                result = result.concat(charBuf[1]);
                result = result.concat(" ");

                if (j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2) {
                    result = result.concat(" ");
                }

                if (rowBuf[j] < 0x20 || rowBuf[j] > 0xD9) {
                    rowBuf[j] = '.';
                }
            }

            for ( /* j INHERITED */; j < BYTES_PER_ROW; j++) {
                rowBuf[j] = ' ';
                result = result.concat("   ");
                if (j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2) {
                    result = result.concat(" ");
                }
            }

            String saveStr = new String(rowBuf, 0, j);
            result = result.concat(" | " + saveStr + " |" + LINE_SEPARATOR);
        }

        return result;
    }

    private static int log16(int value) {
        int bits = Integer.SIZE - Integer.numberOfLeadingZeros(value > 0 ? value - 1 : value);
        int hex = (bits - 1) / 4 + 1;

        return hex;
    }

    private static String buildRowLabel(int byteCount, int labelSize) {
        String result = "";

        for (int c = 0; c < labelSize; c++) {
            int shift = c * 4;
            result = hexChars[((byteCount >> shift) & 0xf)] + result;
        }

        return result;
    }
}

Related

  1. dumpBytes(String headerStr, byte[] bytes)
  2. dumpBytesAsInt(byte[] b)
  3. dumpCodeBytes(byte[] data)
  4. dumphex(byte[] bytes)
  5. dumpHexBytes(byte[] bytes)