Java Utililty Methods ByteBuffer to Hex

List of utility methods to do ByteBuffer to Hex

Description

The list of methods to do ByteBuffer to Hex are organized into topic(s).

Method

StringtoHex(ByteBuffer data)
Convert data from given ByteBuffer to hex
StringBuilder result = new StringBuilder();
int counter = 0;
int b;
while (data.hasRemaining()) {
    if (counter % 16 == 0)
        result.append(String.format("%04X: ", counter));
    b = data.get() & 0xff;
    result.append(String.format("%02X ", b));
...
StringtoHexStr(final ByteBuffer data)
Convert the bytes to a hex string.
final StringBuilder sysex = new StringBuilder();
while (data.position() < data.limit())
    sysex.append(toHexStr(Byte.toUnsignedInt(data.get()))).append(' ');
return sysex.toString();
StringtoHexStream(ByteBuffer data)
Convert data from given ByteBuffer to hex
StringBuilder result = new StringBuilder();
int counter = 0;
int b;
while (data.hasRemaining()) {
    b = data.get() & 0xff;
    result.append(String.format("%02X ", b));
    counter++;
    if (counter % 16 == 0) {
...
StringtoHexString(ByteBuffer bb)
to Hex String
ByteBuffer buffer = bb.duplicate();
StringBuilder hex = new StringBuilder();
while (buffer.hasRemaining()) {
    byte b = buffer.get();
    hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F)));
return hex.toString();
StringtoHexString(ByteBuffer bb)
to Hex String
return appendHex(new StringBuilder(), bb).toString();
StringtoHexString(ByteBuffer bb, boolean withSpaces)
to Hex String
byte[] data = new byte[bb.remaining()];
int position = bb.position();
bb.get(data);
bb.position(position);
return toHexString(data, withSpaces);
StringtoHexString(ByteBuffer buffer)
to Hex String
final StringBuilder r = new StringBuilder(buffer.remaining() * 2);
while (buffer.hasRemaining()) {
    final byte b = buffer.get();
    r.append(HEX_CODE[(b >> 4) & 0xF]);
    r.append(HEX_CODE[(b & 0xF)]);
return r.toString();
StringtoHexString(ByteBuffer buffer)
to Hex String
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
for (int i = buffer.position(); i < buffer.limit(); i++) {
    byte b = buffer.get(i);
    pw.format("%02X ", b);
return sw.toString();
StringtoHexString(ByteBuffer buffer, int size)
Convert a byte buffer to a hexadecimal string for display
return toHexString(buffer, 0, size);
StringtoHexString(ByteBuffer byteBuffer)
Encodes a byte buffer into hex (no spaces) string from the current position.
if (byteBuffer == null) {
    return null;
StringBuilder sb = new StringBuilder();
int pos = byteBuffer.position();
while (byteBuffer.hasRemaining()) {
    String hex = Integer.toHexString(0xFF & byteBuffer.get());
    if (hex.length() == 1) {
...