All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.eBus.util.HexDump Maven / Gradle / Ivy

There is a newer version: 7.4.0
Show newest version
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General
// Public License along with this library; if not, write to the
//
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA
// 02111-1307 USA
//
// The Initial Developer of the Original Code is Charles W. Rapp.
// Portions created by Charles W. Rapp are
// Copyright (C) 2001 - 2005, 2013. Charles W. Rapp.
// All Rights Reserved.
//

package net.sf.eBus.util;

import java.util.Arrays;
import java.util.Formatter;

/**
 * {@link #dump(byte[], String)} generates a standard hexdump
 * textual representation of binary data.
 *
 * @author Charles Rapp
 */

public final class HexDump
{
//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    // Do not allow construction.
    private HexDump()
    {}

    //
    // end of Constructors.
    //-----------------------------------------------------------

    /**
     * Dump out {@code data}'s entire contents starting at
     * byte zero.
     * @param data Generate hex dump for this data region.
     * @param indent Prefix each line with this text.
     * @return a text representation of the given byte array.
     */
    public static String dump(final byte[] data,
                              final String indent)
    {
        return (dump(data, 0, data.length, indent));
    } // end of dump(byte[], String)

    /**
     * Returns a text representation of the given byte array.
     * The output format is:
     * 
     *   
     * 0x00000000:  xx xx xx xx xx xx xx xx   xx xx xx xx xx xx xx xx  "........ ........"
     *   
     * 
* @param data Generate hex dump for this data region. * @param offset Offset into the data array. * @param size Out these many bytes in data array. * @param indent Prefix each line with this text. * @return a text representation of the given byte array. */ public static String dump(final byte[] data, final int offset, final int size, final String indent) { int address; int lineSize; final char[] ascii = new char[BYTES_PER_LINE + SPACE_COUNT]; int remaining; int index1; int index2; int index3; int datum; final Formatter retval = new Formatter(); // Continue until all the data is output. for (index1 = 0, address = 0; index1 < size; index1 += lineSize, address += lineSize) { remaining = (size - index1); lineSize = (remaining < BYTES_PER_LINE ? remaining : BYTES_PER_LINE); // Clear out the line's text representation. Arrays.fill(ascii, ' '); ascii[0] = '"'; // Output the next line. Start by indenting the // line and printing out the address. retval.format("%n"); if (indent != null) { retval.format(indent); } retval.format("0x%08x: ", address); // Now output the raw bytes in hex. Put three blanks // after the eigth byte. for (index2 = 0, index3 = 0; index2 < lineSize; ++index2) { datum = (int) data[offset + index1 + index2]; // Ignore the sign bit by adding 256 to negative // numbers. if (datum < 0) { datum += MAX_BYTE; } if (index2 == BREAK_COLUMN) { retval.format(" "); index3 = 1; } else { retval.format(" "); } retval.format("%02x", datum); // Add this byte's ASCII translation to the // ascii array - but on if it is printable. // Otherwise use a '.' for the byte. ascii[index2 + index3 + 1] = ASCII_CODE[datum]; } ascii[index2 + index3 + 1] = '"'; // Finish up this line by outputing the ascii // representation. // Note: if lineSize < max size, then right justify // the ascii output. if (lineSize < BYTES_PER_LINE) { for (index2 = lineSize; index2 < BYTES_PER_LINE; ++index2) { if (index2 == BREAK_COLUMN) { retval.format(" "); } retval.format(" "); } } retval.format(" %s", new String(ascii)); } return (retval.toString()); } // end of dump(byte[], String) //--------------------------------------------------------------- // Member data. // //----------------------------------------------------------- // Constants. // private static final int BYTES_PER_LINE = 16; private static final int BREAK_COLUMN = 8; private static final int SPACE_COUNT = 3; private static final int MAX_BYTE = 256; private static final char[] ASCII_CODE = { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }; } // end of class HexDump // // CHANGE LOG // $Log: HexDump.java,v $ // Revision 1.3 2006/05/06 19:56:52 charlesr // Corrected ASCII_CODE. // // Revision 1.2 2006/04/28 18:41:24 charlesr // Fixed problem where byte > 127. // // Revision 1.1 2006/04/28 18:20:56 charlesr // Initial revision //




© 2015 - 2024 Weber Informatics LLC | Privacy Policy