Java ByteBuffer to Hex toHexStr(final ByteBuffer data)

Here you can find the source of toHexStr(final ByteBuffer data)

Description

Convert the bytes to a hex string.

License

LGPL

Parameter

Parameter Description
data The data to convert

Return

The hex string

Declaration

public static String toHexStr(final ByteBuffer data) 

Method Source Code


//package com.java2s;
// Licensed under LGPLv3 - http://www.gnu.org/licenses/lgpl-3.0.txt

import java.nio.ByteBuffer;

public class Main {
    /**/*  w ww . j a v a 2 s  .co  m*/
     * Convert the bytes to a hex string.
     *
     * @param data The data to convert
     * @return The hex string
     */
    public static String toHexStr(final int[] data) {
        final StringBuilder sysex = new StringBuilder();
        for (final int d : data)
            sysex.append(toHexStr(d)).append(' ');
        return sysex.toString();
    }

    /**
     * Convert the bytes to a hex string. Rewinds the buffer and adds the bytes from the beginning
     * till the capacity.
     *
     * @param data The data to convert
     * @return The hex string
     */
    public static String toHexStr(final ByteBuffer data) {
        final StringBuilder sysex = new StringBuilder();
        while (data.position() < data.limit())
            sysex.append(toHexStr(Byte.toUnsignedInt(data.get()))).append(' ');
        return sysex.toString();
    }

    /**
     * Convert the bytes to a hex string
     *
     * @param data The data to convert
     * @return The hex string
     */
    public static String toHexStr(final byte[] data) {
        final StringBuilder sysex = new StringBuilder();
        for (final byte d : data)
            sysex.append(toHexStr(Byte.toUnsignedInt(d))).append(' ');
        return sysex.toString();
    }

    /**
     * Convert the byte to a hex string
     *
     * @param number The value to convert
     * @return The hex string
     */
    public static String toHexStr(final int number) {
        return String.format("%02X", Integer.valueOf(number));
    }
}

Related

  1. toHex(ByteBuffer bb)
  2. toHex(ByteBuffer bb)
  3. toHex(ByteBuffer buf)
  4. toHex(ByteBuffer buffer)
  5. toHex(ByteBuffer data)
  6. toHexStream(ByteBuffer data)
  7. toHexString(ByteBuffer bb)
  8. toHexString(ByteBuffer bb)
  9. toHexString(ByteBuffer bb, boolean withSpaces)