Convert a byte buffer to a hexadecimal string for display - Java java.nio

Java examples for java.nio:ByteBuffer Convert

Description

Convert a byte buffer to a hexadecimal string for display

Demo Code

/*//  w w  w  .ja v  a 2  s.  c  o m
Copyright (c) 2005 Health Market Science, Inc.

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

You can contact Health Market Science at info@healthmarketscience.com
or at the following address:

Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
 */
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class Main{
    public static void main(String[] argv) throws Exception{
        byte[] array = new byte[]{34,35,36,37,37,37,67,68,69};
        System.out.println(toHexString(array));
    }
    private static final String[] HEX_CHARS = new String[] { "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
    private static final int NUM_BYTES_PER_BLOCK = 4;
    private static final int NUM_BYTES_PER_LINE = 24;
    /**
     * Convert a byte buffer to a hexadecimal string for display
     * @param buffer Buffer to display, starting at offset 0
     * @param size Number of bytes to read from the buffer
     * @return The display String
     */
    public static String toHexString(ByteBuffer buffer, int size) {
        return toHexString(buffer, 0, size);
    }
    /**
     * Convert a byte array to a hexadecimal string for display
     * @param array byte array to display, starting at offset 0
     * @return The display String
     */
    public static String toHexString(byte[] array) {
        return toHexString(ByteBuffer.wrap(array), 0, array.length);
    }
    /**
     * Convert a byte buffer to a hexadecimal string for display
     * @param buffer Buffer to display, starting at offset 0
     * @param offset Offset at which to start reading the buffer
     * @param size Number of bytes to read from the buffer
     * @return The display String
     */
    public static String toHexString(ByteBuffer buffer, int offset, int size) {
        return toHexString(buffer, offset, size, true);
    }
    /**
     * Convert a byte buffer to a hexadecimal string for display
     * @param buffer Buffer to display, starting at offset 0
     * @param offset Offset at which to start reading the buffer
     * @param size Number of bytes to read from the buffer
     * @param formatted flag indicating if formatting is required
     * @return The display String
     */
    public static String toHexString(ByteBuffer buffer, int offset,
            int size, boolean formatted) {

        StringBuilder rtn = new StringBuilder();
        int position = buffer.position();
        buffer.position(offset);

        for (int i = 0; i < size; i++) {
            byte b = buffer.get();
            byte h = (byte) (b & 0xF0);
            h = (byte) (h >>> 4);
            h = (byte) (h & 0x0F);
            rtn.append(HEX_CHARS[h]);
            h = (byte) (b & 0x0F);
            rtn.append(HEX_CHARS[h]);

            int next = (i + 1);
            if (formatted && (next < size)) {
                if ((next % NUM_BYTES_PER_LINE) == 0) {

                    rtn.append("\n");

                } else {

                    rtn.append(" ");

                    if ((next % NUM_BYTES_PER_BLOCK) == 0) {
                        rtn.append(" ");
                    }
                }
            }
        }

        buffer.position(position);
        return rtn.toString();
    }
    /**
     * Convert the given number of bytes from the given database page to a
     * hexidecimal string for display.
     */
    public static String toHexString(DatabaseImpl db, int pageNumber,
            int size) throws IOException {
        ByteBuffer buffer = db.getPageChannel().createPageBuffer();
        db.getPageChannel().readPage(buffer, pageNumber);
        return toHexString(buffer, size);
    }
}

Related Tutorials