Java Dump Byte Array dumpHex(byte[] data)

Here you can find the source of dumpHex(byte[] data)

Description

Dump an the content of an array of byte as a hex dump

License

Apache License

Parameter

Parameter Description
data The array of byte

Return

The dump

Declaration

public static String dumpHex(byte[] data) 

Method Source Code

//package com.java2s;
/**//from w  w  w  .j  a  v  a2 s. c  om
 *   Copyright 2006 Alcatel, OSP.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

import java.io.*;

public class Main {
    /**
     * Dump an the content of an array of byte as a hex dump
     *
     * @param  data  The array of byte
     * @return       The dump
     */
    public static String dumpHex(byte[] data) {
        StringBuffer result = new StringBuffer();
        ByteArrayInputStream buf = new ByteArrayInputStream(data);
        int lineBytes = 0;
        int i;
        byte[] b = new byte[16];
        String s;

        while (buf.available() > 0) {
            b[lineBytes] = (byte) buf.read();
            int v = b[lineBytes];
            v = v < 0 ? 256 + v : v;
            s = Integer.toHexString(v);
            if (s.length() < 2) {
                s = "0" + s;
            }

            result.append(s.toUpperCase() + " ");

            lineBytes++;
            if (lineBytes == b.length) {
                dumpAsciiChars(b, b.length);
                lineBytes = 0;
            }
            // if
        }
        // while

        if (lineBytes > 1) {
            // unfilled line?

            for (i = lineBytes; i < b.length; i++) {
                // pad out.
                result.append("   ");
            }
            dumpAsciiChars(b, lineBytes);
            // dump chars.
        }
        // if

        result.append('\n');
        return result.toString();
    }

    /**
     * Print a dump hex of the byte of array
     *
     * @param  b     The byte of array
     * @param  blen  The number of byte to dump
     */
    private static void dumpAsciiChars(byte[] b, int blen) {
        int lineBytes;

        System.out.print("            ");
        for (lineBytes = 0; lineBytes < blen; lineBytes++) {
            if ((b[lineBytes] >= 32) && (b[lineBytes] <= 126)) {
                System.out.print((char) b[lineBytes]);
            } else {
                System.out.print(".");
            }
        }
        // for

        System.out.println();
    }
}

Related

  1. dumpBytes(PrintStream printStream, byte bytes[])
  2. dumpBytesAsString(ByteArrayOutputStream baos)
  3. dumpBytesToFile(byte[] bytes, String outputFile)
  4. dumpClass(String file, byte[] bytes)
  5. dumpFileIntoByteArray(File f)
  6. dumpHex(PrintStream out, byte[] data, int offset, int length)
  7. dumpHex(String _string, String _encoding)
  8. dumpHex(String value, String encoding)