Java ByteBuffer convert to String hexadecimal
import java.nio.ByteBuffer; import java.util.Arrays; public class Main { public static void main(String[] argv) throws Exception { byte[] byteArray = new byte[] { 65, 66, 67, 68, 69 }; ByteBuffer bb = ByteBuffer.wrap(byteArray); String s = toString(bb);//from www. j a va 2 s . c om System.out.println(s); } /** * Converts the contents of the specified {@link ByteBuffer} to a string, * which is formatted similarly to the * output * of the {@link Arrays#toString()} method. * * @param buffer The buffer. * @return The string. */ public static String toString(ByteBuffer buffer) { StringBuilder builder = new StringBuilder("["); int limit = buffer.limit(); for (int index = 0; index < limit; index++) { String hex = Integer.toHexString(buffer.get() & 0xFF) .toUpperCase(); if (hex.length() == 1) { hex = "0" + hex; } builder.append("0x").append(hex); if (index != limit - 1) { builder.append(", "); } } builder.append("]"); return builder.toString(); } }