Java Byte Array Dump dumpBytes(byte[] buffer)

Here you can find the source of dumpBytes(byte[] buffer)

Description

Get the text representation of the specified byte array.

License

Open Source License

Parameter

Parameter Description
buffer the byte array

Return

the text representation of the specified byte array

Declaration

public static String dumpBytes(byte[] buffer) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2012 by Min Cai (min.cai.china@gmail.com).
 *
 * This file is part of the PickaPack library.
 *
 * PickaPack 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.//from   ww  w .  jav a2  s.c om
 *
 * PickaPack 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 PickaPack. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

public class Main {
    /**
     * Get the text representation of the specified byte array.
     *
     * @param buffer the byte array
     * @return the text representation of the specified byte array
     */
    public static String dumpBytes(byte[] buffer) {
        return dumpBytes(buffer, 0, buffer.length);
    }

    /**
     * Get the text representation of the specified byte array.
     *
     * @param buffer the byte array
     * @param offset the offset
     * @param size the size
     * @return the text representation of the specified byte array
     */
    public static String dumpBytes(byte[] buffer, int offset, int size) {
        if (buffer == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        for (int i = offset; i < size; i++) {
            if (i != 0 && i % 8 == 0) {
                sb.append("\n");
            }
            sb.append(String.format("%08x%s", buffer[i], (i < (size - 1) ? " " : "")));
        }

        return sb.toString();
    }
}

Related

  1. dumpByteArrayAsInts(byte[] arr)
  2. dumpBytes(byte bb[])
  3. dumpBytes(byte[] a)
  4. dumpBytes(byte[] b)
  5. dumpBytes(byte[] buffer)
  6. dumpBytes(byte[] bytes)
  7. dumpBytes(byte[] bytes)
  8. dumpBytes(byte[] bytes)
  9. dumpBytes(byte[] bytes, int maxLen)