Java Convert via ByteBuffer hexDump(byte... b)

Here you can find the source of hexDump(byte... b)

Description

Returns the hex dump of the given byte array as 16 bytes per line

License

Open Source License

Parameter

Parameter Description
b the byte array

Return

A string with the hex dump

Declaration

public static String hexDump(byte... b) 

Method Source Code


//package com.java2s;
/*//  w  w  w .ja  v  a  2  s .  co  m
 * This file is part of l2jserver2 <l2jserver2.com>.
 *
 * l2jserver2 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.
 *
 * l2jserver2 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 l2jserver2.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Returns the hex dump of the given byte array as 16 bytes per line
     * 
     * @param b
     *            the byte array
     * @return A string with the hex dump
     */
    public static String hexDump(byte... b) {
        if (b == null)
            return "";
        StringBuffer buf = new StringBuffer();
        int size = b.length;
        for (int i = 0; i < size; i++) {
            if ((i + 1) % 16 == 0) {
                buf.append(zeropad(Integer.toHexString(byteToUInt(b[i])).toUpperCase(), 2));
                buf.append('\n');
            } else {
                buf.append(zeropad(Integer.toHexString(byteToUInt(b[i])).toUpperCase(), 2));
                buf.append(" ");
            }
        }
        return buf.toString();
    }

    /**
     * Returns the hex dump of the given byte array as 16 bytes per line
     * 
     * @param buffer
     *            the byte buffer
     * @return A string with the hex dump
     */
    public static String hexDump(ByteBuffer buffer) {
        if (buffer == null)
            return "";
        StringBuffer buf = new StringBuffer();
        int size = buffer.remaining();
        for (int i = 0; i < size; i++) {
            if ((i + 1) % 16 == 0) {
                buf.append(zeropad(Integer.toHexString(byteToUInt(buffer.get(buffer.position() + i))).toUpperCase(),
                        2));
                buf.append('\n');
            } else {
                buf.append(zeropad(Integer.toHexString(byteToUInt(buffer.get(buffer.position() + i))).toUpperCase(),
                        2));
                buf.append(" ");
            }
        }
        return buf.toString();
    }

    public static String zeropad(String number, int size) {
        if (number.length() >= size)
            return number;
        return repeat("0", size - number.length()) + number;
    }

    /**
     * Returns the unsigned value of a byte
     * 
     * @param the
     *            byte witch u want to convert
     */
    public static int byteToUInt(byte b) {
        return b & 0xFF;
    }

    public static String repeat(String str, int repeat) {
        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < repeat; i++)
            buf.append(str);

        return buf.toString();
    }
}

Related

  1. hexDump(byte[] buffer)
  2. hexStringToBytes(String hexString)
  3. hexStrToStr(String hexStr)
  4. hexToAscii(byte[] src)