Java ByteBuffer to Hex toHexString(ByteBuffer buffer, int size)

Here you can find the source of toHexString(ByteBuffer buffer, int size)

Description

Convert a byte buffer to a hexadecimal string for display

License

Open Source License

Parameter

Parameter Description
buffer Buffer to display, starting at offset 0
size Number of bytes to read from the buffer

Return

The display String

Declaration

public static String toHexString(ByteBuffer buffer, int size) 

Method Source Code

//package com.java2s;
/*//from  w  ww  .  j a v a2s.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.nio.ByteBuffer;

public class Main {
    private static final String[] HEX_CHARS = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
            "B", "C", "D", "E", "F" };

    /**
     * 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]);

            if (formatted == true) {
                rtn.append(" ");

                if ((i + 1) % 4 == 0) {
                    rtn.append(" ");
                }
                if ((i + 1) % 24 == 0) {
                    rtn.append("\n");
                }
            }
        }

        buffer.position(position);
        return rtn.toString();
    }
}

Related

  1. toHexString(ByteBuffer bb)
  2. toHexString(ByteBuffer bb)
  3. toHexString(ByteBuffer bb, boolean withSpaces)
  4. toHexString(ByteBuffer buffer)
  5. toHexString(ByteBuffer buffer)
  6. toHexString(ByteBuffer byteBuffer)
  7. toHexString(ByteBuffer byteBuffer)
  8. toHexString(final ByteBuffer buffer)
  9. toHexValue(ByteBuffer buffer)