Java Dump Byte Array dumpHex(PrintStream out, byte[] data, int offset, int length)

Here you can find the source of dumpHex(PrintStream out, byte[] data, int offset, int length)

Description

Dumps an array of byte to the output string as a sequence of hexadecimal digits.

License

Open Source License

Parameter

Parameter Description
out The output stream
data The data to dump
offset The start location within the data
length The length of data to dump

Declaration

static void dumpHex(PrintStream out, byte[] data, int offset, int length) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of OpenNMS(R).//from   www .j  a va  2  s  .  c  o m
 *
 * Copyright (C) 2011 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2011 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

import java.io.PrintStream;

public class Main {
    /**
     * Dumps an array of byte to the output string as a sequence of hexadecimal
     * digits.
     * 
     * @param out
     *            The output stream
     * @param data
     *            The data to dump
     * @param offset
     *            The start location within the data
     * @param length
     *            The length of data to dump
     * 
     */
    static void dumpHex(PrintStream out, byte[] data, int offset, int length) {
        if ((offset + length) > data.length)
            return;

        int cnt = 0;
        while (length > 0) {
            byte b = data[offset];
            out.print("0x");
            out.print(Integer.toHexString((b >> 4) & 0xf));
            out.print(Integer.toHexString(b & 0xf));
            out.print(" ");
            --length;
            offset++;

            if ((cnt++ % 16) == 0 && cnt != 1)
                out.println("");
        }
    }
}

Related

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