public static String toHex(ByteBuffer data){
  StringBuilder result=new StringBuilder();
  int counter=0;
  int b;
  while (data.hasRemaining()) {
    if (counter % 16 == 0) {
      result.append(String.format("%04X: ",counter));
    }
    b=data.get() & 0xff;
    result.append(String.format("%02X ",b));
    counter++;
    if (counter % 16 == 0) {
      result.append("  ");
      toText(data,result,16);
      result.append("\n");
    }
  }
  int rest=counter % 16;
  if (rest > 0) {
    for (int i=0; i < 17 - rest; i++) {
      result.append("   ");
    }
    toText(data,result,rest);
  }
  return result.toString();
}

public static String toHex ( ByteBuffer data ) { StringBuilder result = new StringBuilder ( ) ; int counter = 0 ; int b ; while ( data . hasRemaining ( ) ) { if ( counter % 16 == 0 ) { result . append ( String . format ( "%04X: " , counter ) ) ; } b = data . get ( ) & 0xff ; result . append ( String . format ( "%02X " , b ) ) ; counter ++ ; if ( counter % 16 == 0 ) { result . append ( "  " ) ; toText ( data , result , 16 ) ; result . append ( "\n" ) ; } } int rest = counter % 16 ; if ( rest > 0 ) { for ( int i = 0 ; i < 17 - rest ; i ++ ) { result . append ( "   " ) ; } toText ( data , result , rest ) ; } return result . toString ( ) ; }
convert data from given bytebuffer to hex
