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

StringasHexString(ByteBuffer buffer)
Represent as a String all the bytes (in hex format) in a ByteBuffer.
final int charsPerLine = 16;
StringBuilder asString = new StringBuilder();
StringBuilder currentLine = new StringBuilder();
StringBuilder all = new StringBuilder();
boolean processedFirst = false;
int currentChar = 0;
int length = buffer.limit();
int eobyte = 0;
...
StringBuilderasHexString(StringBuilder temp, ByteBuffer buffer)
as Hex String
int limit = buffer.limit();
for (int i = buffer.position(); i < limit; i++)
    temp.append(
            i + 1 == limit ? String.format("%02x", buffer.get(i)) : String.format("%02x ", buffer.get(i)));
return temp;
StringbyteBufferToHex(ByteBuffer buffer)
Returns a hexadecimal representation of the bytes written to ByteBuffer (i.e.
ByteBuffer tmp = buffer.duplicate();
tmp.flip();
byte[] bytes = new byte[tmp.remaining()];
tmp.get(bytes);
return bytesToHex(bytes);
StringbytesToHex(ByteBuffer bytes)
bytes To Hex
char[] hexChars = new char[bytes.limit() * 2];
for (int j = 0; j < bytes.limit(); j++) {
    int v = bytes.get() & 0xFF;
    hexChars[j * 2] = hexArray[v >>> 4];
    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
return new String(hexChars);
StringbytesToHex(ByteBuffer bytes)
bytes To Hex
StringBuilder sb = new StringBuilder();
for (int i = bytes.position() + bytes.arrayOffset(); i < bytes.limit() + bytes.arrayOffset(); i++) {
    int bint = bytes.array()[i] & 0xff;
    if (bint <= 0xF)
        sb.append("0");
    sb.append(Integer.toHexString(bint));
return sb.toString();
...
StringbytesToHexString(ByteBuffer b)
bytes To Hex String
StringBuilder result = new StringBuilder();
for (int i = 0; i < b.remaining(); ++i) {
    final int v = b.array()[b.arrayOffset() + b.position() + i] & 0xff;
    if (i > 0) {
        result.append(" ");
    if (v < 16) {
        result.append("0");
...
StringBuffertoHex(ByteBuffer bb)
to Hex
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bb.limit(); i++) {
    sb.append(Integer.toString((bb.get(i) & 0xff) + 0x100, 16).substring(1));
return sb;
StringtoHex(ByteBuffer bb)
to Hex
StringBuilder sb = new StringBuilder("[ ");
while (bb.hasRemaining()) {
    sb.append(String.format("%02X ", bb.get()));
sb.append("]");
return sb.toString();
StringtoHex(ByteBuffer buf)
Interprets the bytes inside the given buffer as a hexadecimal string.
int orig = buf.position();
StringBuilder sb = new StringBuilder();
while (buf.remaining() > 0) {
    byte b = buf.get();
    sb.append(HEX_CHARS[(b & 0xF0) >>> 4]);
    sb.append(HEX_CHARS[b & 0x0F]);
buf.position(orig);
...
StringtoHex(ByteBuffer buffer)
to Hex
StringBuilder result = new StringBuilder();
int pos = buffer.position();
buffer.position(0);
for (int i = buffer.limit(); --i >= 0;) {
    byte b = buffer.get();
    result.append(HEX.charAt((b >> 4) & 0xF));
    result.append(HEX.charAt(b & 0xF));
    result.append(' ');
...