ByteBufferDumper { public static String toHex(ByteBuffer buffer) { StringBuilder out = new StringBuilder(buffer.remaining() * 2); int pos = buffer.position(); while (buffer.hasRemaining()) { int byteValue = buffer.get() & 0xFF; out.append((char) (HEX_CHAR[(byteValue & 0x00F0) >> 4])) .append((char) ((HEX_CHAR[byteValue & 0x000F]) - 0)); } buffer.position(pos); return out.toString(); }  static String dump(ByteBuffer buffer, int nbBytes, boolean toAscii); static String dump(ByteBuffer buffer); static String toHex(ByteBuffer buffer); static ByteBuffer fromHexString(String hex);  }
@Test public void toHex() { ByteBuffer bb = ByteBuffer.allocate(4); bb.put((byte) 0); bb.put((byte) 1); bb.put((byte) 2); bb.put((byte) 254); bb.flip(); assertEquals("000102FE", ByteBufferDumper.toHex(bb)); }