Java Hex Calculate toHexDump(byte[] buffer, int offset, int length, boolean hex, boolean ascii)

Here you can find the source of toHexDump(byte[] buffer, int offset, int length, boolean hex, boolean ascii)

Description

Creates a hexdump-like output for a given data buffer.

License

Open Source License

Parameter

Parameter Description
buffer The data buffer.
offset The offset into the buffer to start with.
length The number of bytes to display.
hex Show hex version?
ascii Show ASCII version?

Return

The hexdump text (this will be an empty character sequence if both hex and ascii are false).

Declaration

public static CharSequence toHexDump(byte[] buffer, int offset, int length, boolean hex, boolean ascii) 

Method Source Code

//package com.java2s;
/**//  w  ww  . jav  a 2 s. c o  m
 * This file is protected by Copyright. Please refer to the COPYRIGHT file
 * distributed with this source distribution.
 *
 * This file is part of REDHAWK.
 *
 * REDHAWK 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 3 of the License, or (at your
 * option) any later version.
 *
 * REDHAWK 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 program.  If not, see http://www.gnu.org/licenses/.
 */

public class Main {
    /** Creates a hexdump-like output for a given data buffer. This is identical to
     *  <tt>toHexDump(buffer,offset,length,true,true,true,"\n")</tt>.
     *  @param buffer  The data buffer.
     *  @param offset  The offset into the buffer to start with.
     *  @param length  The number of bytes to display.
     *  @return The hexdump text (this will be an empty character sequence if both <tt>hex</tt> and
     *          <tt>ascii</tt> are false).
     */
    public static CharSequence toHexDump(byte[] buffer, int offset, int length) {
        return toHexDump(buffer, offset, length, true, true, true, "\n");
    }

    /** Creates a hexdump-like output for a given data buffer. This is identical to
     *  <tt>toHexDump(buffer,offset,length,hex,ascii,true,"\n")</tt>.
     *  @param buffer  The data buffer.
     *  @param offset  The offset into the buffer to start with.
     *  @param length  The number of bytes to display.
     *  @param hex     Show hex version?
     *  @param ascii   Show ASCII version?
     *  @return The hexdump text (this will be an empty character sequence if both <tt>hex</tt> and
     *          <tt>ascii</tt> are false).
     */
    public static CharSequence toHexDump(byte[] buffer, int offset, int length, boolean hex, boolean ascii) {
        return toHexDump(buffer, offset, length, hex, ascii, true, "\n");
    }

    /** Creates a hexdump-like output for a given data buffer.
     *  @param buffer  The data buffer.
     *  @param offset  The offset into the buffer to start with.
     *  @param length  The number of bytes to display.
     *  @param hex     Show hex version?
     *  @param ascii   Show ASCII version?
     *  @param index   Show index values?
     *  @param eol     End of line character (usually "\n").
     *  @return The hexdump text (this will be an empty character sequence if both <tt>hex</tt> and
     *          <tt>ascii</tt> are false).
     */
    public static CharSequence toHexDump(byte[] buffer, int offset, int length, boolean hex, boolean ascii,
            boolean index, String eol) {
        StringBuilder str = new StringBuilder();
        StringBuilder txt = new StringBuilder();

        if (hex) {
            for (int i = offset, j = 0; j < length;) {
                if (index) {
                    str.append(j).append(":  \t");
                }

                for (int k = 0; k < 16; i++, j++, k++) {
                    if (j >= length) {
                        str.append("   ");
                        txt.append(" ");
                    } else {
                        int b = buffer[i] & 0xFF;
                        char c = ((b < 0x20) || (b >= 0x7F)) ? '.' : (char) b;
                        int n1 = b >> 4;
                        int n2 = b & 0xF;
                        int q1 = n1 & ((n1 << 1) | (n1 << 2)) & 0x8; // 0x0 if <0xA, 0x8 if >=0xA
                        int q2 = n2 & ((n2 << 1) | (n2 << 2)) & 0x8; // 0x0 if <0xA, 0x8 if >=0xA
                        char c1 = (char) (n1 + 0x30 + q1 - (q1 >> 3)); // q-(q>>3) = 0x8-0x1 = 'A'-'9'-1 if >=0xA
                        char c2 = (char) (n2 + 0x30 + q2 - (q2 >> 3)); // q-(q>>3) = 0x8-0x1 = 'A'-'9'-1 if >=0xA
                        str.append(c1).append(c2).append(' ');
                        txt.append(c);
                    }
                    if (k == 7)
                        str.append(' ');
                }
                if (ascii) {
                    str.append("  |  ").append(txt);
                    txt.setLength(0);
                }
                str.append(eol);
            }
        } else if (ascii) {
            for (int i = offset, j = 0; j < length;) {
                if (index) {
                    str.append(j).append(":  \t");
                }

                for (int k = 0; k < 80; i++, j++, k++) {
                    if (j >= length) {
                        str.append(" ");
                    } else {
                        int b = buffer[i] & 0xFF;
                        char c = ((b < 0x20) || (b >= 0x7F)) ? '.' : (char) b;
                        str.append(c);
                    }
                }
                str.append(eol);
            }
        }
        return str;
    }

    /** Appends a value to a string buffer if not null.
     *  @param str  The string buffer
     *  @param name The name of the value.
     *  @param val  The value.
     */
    public static void append(StringBuilder str, String name, Object val) {
        if (val != null)
            str.append(name).append(val);
    }

    /** Appends a value to a string buffer if not null.
     *  @param str    The string buffer
     *  @param prefix Prefix to include.
     *  @param val    The value.
     *  @param suffix Suffix to include.
     */
    public static void append(StringBuilder str, String prefix, Object val, String suffix) {
        if (val != null)
            str.append(prefix).append(val).append(suffix);
    }
}

Related

  1. toHexDigit(int number)
  2. toHexDigit(int value, int digitPosition)
  3. toHexDigits(byte[] bytes)
  4. toHexDigits(int value)
  5. toHexDump(byte[] ab, int cBytesPerLine)
  6. toHexDump(byte[] bytes)
  7. toHexEscape(final int u0)
  8. toHexFilter(String inAscii)
  9. toHexFromBin(final String binSymbols)