Java ByteBuffer to Hex toHexString(final ByteBuffer buffer)

Here you can find the source of toHexString(final ByteBuffer buffer)

Description

Creates an hexadecimal string representation of the given ByteBuffer.

License

Open Source License

Parameter

Parameter Description
buffer the buffer

Return

the hexadecimal string

Declaration

public static String toHexString(final ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  j a  v  a  2s. c o m
 *
 * JMeta - Meta's java implementation
 *
 * Copyright (C) 2013-2015 Pablo Joubert
 * Copyright (C) 2013-2015 Thomas Lavocat
 * Copyright (C) 2013-2015 Nicolas Michon
 *
 * This file is part of JMeta.
 *
 * JMeta is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * JMeta 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Hexadecimal chars for utility.
     */
    protected static final char[] HEX_CHAR_ARRAY = "0123456789abcdef".toCharArray();

    /**
     * Creates an hexadecimal string representation of the given ByteBuffer.
     *
     * @param buffer the buffer
     * @return the hexadecimal string
     */
    public static String toHexString(final ByteBuffer buffer) {
        ByteBuffer buf = buffer.asReadOnlyBuffer();
        buf.rewind();
        char[] hexChars = new char[buffer.limit() * 2];

        for (int j = 0; j < buf.limit(); j++) {
            int v = buf.get() & 0xFF;
            hexChars[j * 2] = HEX_CHAR_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_CHAR_ARRAY[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Related

  1. toHexString(ByteBuffer buffer)
  2. toHexString(ByteBuffer buffer)
  3. toHexString(ByteBuffer buffer, int size)
  4. toHexString(ByteBuffer byteBuffer)
  5. toHexString(ByteBuffer byteBuffer)
  6. toHexValue(ByteBuffer buffer)