Here you can find the source of readHexString(ByteBuffer buffer, int nrBytes)
public static String readHexString(ByteBuffer buffer, int nrBytes)
//package com.java2s; //License from project: Apache License import java.nio.ByteBuffer; public class Main { private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; public static String readHexString(ByteBuffer buffer, int nrBytes) { byte[] bytes = new byte[nrBytes]; for (int i = bytes.length - 1; i >= 0; i--) { bytes[i] = buffer.get();/* ww w . j a v a 2 s .c o m*/ } return toHexString(bytes); } public static String toHexString(byte value) { return new String(new char[] { HEX[(value >>> 4) & 0xf], HEX[(value >>> 0) & 0xf] }); } public static String toHexString(byte[] bytes) { return toHexString(bytes, 0, bytes.length, 0); } public static String toHexString(byte[] bytes, int offset, int length, int spacing) { StringBuilder sb = new StringBuilder(); for (int ix = offset; ix < offset + length; ix++) { sb.append(HEX[(bytes[ix] >>> 4) & 0xf]); sb.append(HEX[bytes[ix] & 0xf]); if (spacing > 0 && ix % spacing == spacing - 1 && ix < offset + length - 1) { sb.append(' '); } } return sb.toString(); } public static String toHexString(ByteBuffer byteBuffer) { byteBuffer.rewind(); return toHexString(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.remaining(), 4); } public static String toHexString(int value) { return new String(new char[] { HEX[(value >>> 28) & 0xf], HEX[(value >>> 24) & 0xf], HEX[(value >>> 20) & 0xf], HEX[(value >>> 16) & 0xf], HEX[(value >>> 12) & 0xf], HEX[(value >>> 8) & 0xf], HEX[(value >>> 4) & 0xf], HEX[(value >>> 0) & 0xf] }); } public static String toHexString(short value) { return new String(new char[] { HEX[(value >>> 12) & 0xf], HEX[(value >>> 8) & 0xf], HEX[(value >>> 4) & 0xf], HEX[(value >>> 0) & 0xf] }); } }