Java Utililty Methods ByteBuffer to String

List of utility methods to do ByteBuffer to String

Description

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

Method

StringbyteBufferToString(ByteBuffer buf)
Parses a ByteBuffer into a String .
StringBuilder sb = new StringBuilder();
for (int k = 0; k < buf.limit() / 4; k++) {
    if (k != 0) {
        sb.append(" ");
    sb.append(buf.getInt());
return sb.toString();
...
StringbyteBufferToString(ByteBuffer buffer)
byte Buffer To String
StringBuffer sb = new StringBuffer("[");
if (buffer.hasRemaining())
    sb.append(buffer.get());
while (buffer.hasRemaining())
    sb.append(", ").append(buffer.get());
sb.append("]");
buffer.rewind();
return new String(sb);
...
StringbyteBufferToString(ByteBuffer buffer)
convert byte buffer to string and clean
StringBuilder sb = new StringBuilder();
while (buffer.hasRemaining()) {
    sb.append((char) buffer.get());
buffer.clear();
return sb.toString();
StringbyteBufferToString(ByteBuffer buffer)
byte Buffer To String
String data = "";
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
try {
    int old_position = buffer.position();
    data = decoder.decode(buffer).toString();
    buffer.position(old_position);
} catch (Exception e) {
    e.printStackTrace();
...
StringbyteBufferToString(ByteBuffer buffer)
byte Buffer To String
StringBuilder out = new StringBuilder();
int j = 32;
for (int i = 0; i < buffer.capacity(); i++) {
    j++;
    if (j > 7) {
        j = 0;
        out.append("\n[").append(i).append("] ");
    out.append(buffer.get(i)).append(" ");
return out.toString();
StringbyteBufferToString(ByteBuffer buffer)
Takes a ByteBuffer in read mode and converts it to a comma separated string of Hex encoded bytes.
StringBuilder builder = new StringBuilder(buffer.remaining() * 2);
while (buffer.hasRemaining()) {
    String formatted = String.format("%02X", Byte.valueOf(buffer.get()));
    builder.append(formatted).append(',');
buffer.rewind();
return builder.toString();
StringByteBufferToString(ByteBuffer buffer, Charset charset)
Byte Buffer To String
byte[] bytes;
if (buffer.hasArray()) {
    bytes = buffer.array();
} else {
    bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
return new String(bytes, charset);
...
StringbyteBufferToString(ByteBuffer buffer, String encoding)
Translate the given buffer into a string
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return (new String(bytes, encoding));
StringbyteBufferToString(ByteBuffer stringBuf)
byte Buffer To String
byte[] bytes = new byte[stringBuf.remaining() - 1];
stringBuf.get(bytes);
return new String(bytes);
StringbytesToString(ByteBuffer buf, int off, int len)
Convert from bytes to a string.
if (buf == null)
    throw new IllegalArgumentException("null byte buffer provided");
byte[] bbuf = new byte[len];
buf.position(off);
buf.get(bbuf);
char[] cbuf = new char[len];
for (int i = 0; i < bbuf.length; i++) {
    cbuf[i] = (char) bbuf[i];
...