public static String dumpAsHex(byte[] byteBuffer,int length){
  StringBuilder outputBuilder=new StringBuilder(length * 4);
  int p=0;
  int rows=length / 8;
  for (int i=0; (i < rows) && (p < length); i++) {
    int ptemp=p;
    for (int j=0; j < 8; j++) {
      String hexVal=Integer.toHexString(byteBuffer[ptemp] & 0xff);
      if (hexVal.length() == 1) {
        hexVal="0" + hexVal;
      }
      outputBuilder.append(hexVal + " ");
      ptemp++;
    }
    outputBuilder.append("    ");
    for (int j=0; j < 8; j++) {
      int b=0xff & byteBuffer[p];
      if (b > 32 && b < 127) {
        outputBuilder.append((char)b + " ");
      }
 else {
        outputBuilder.append(". ");
      }
      p++;
    }
    outputBuilder.append("\n");
  }
  int n=0;
  for (int i=p; i < length; i++) {
    String hexVal=Integer.toHexString(byteBuffer[i] & 0xff);
    if (hexVal.length() == 1) {
      hexVal="0" + hexVal;
    }
    outputBuilder.append(hexVal + " ");
    n++;
  }
  for (int i=n; i < 8; i++) {
    outputBuilder.append("   ");
  }
  outputBuilder.append("    ");
  for (int i=p; i < length; i++) {
    int b=0xff & byteBuffer[i];
    if (b > 32 && b < 127) {
      outputBuilder.append((char)b + " ");
    }
 else {
      outputBuilder.append(". ");
    }
  }
  outputBuilder.append("\n");
  return outputBuilder.toString();
}

public static String dumpAsHex ( byte [ ] byteBuffer , int length ) { StringBuilder outputBuilder = new StringBuilder ( length * 4 ) ; int p = 0 ; int rows = length / 8 ; for ( int i = 0 ; ( i < rows ) && ( p < length ) ; i ++ ) { int ptemp = p ; for ( int j = 0 ; j < 8 ; j ++ ) { String hexVal = Integer . toHexString ( byteBuffer [ ptemp ] & 0xff ) ; if ( hexVal . length ( ) == 1 ) { hexVal = "0" + hexVal ; } outputBuilder . append ( hexVal + " " ) ; ptemp ++ ; } outputBuilder . append ( "    " ) ; for ( int j = 0 ; j < 8 ; j ++ ) { int b = 0xff & byteBuffer [ p ] ; if ( b > 32 && b < 127 ) { outputBuilder . append ( ( char ) b + " " ) ; } else { outputBuilder . append ( ". " ) ; } p ++ ; } outputBuilder . append ( "\n" ) ; } int n = 0 ; for ( int i = p ; i < length ; i ++ ) { String hexVal = Integer . toHexString ( byteBuffer [ i ] & 0xff ) ; if ( hexVal . length ( ) == 1 ) { hexVal = "0" + hexVal ; } outputBuilder . append ( hexVal + " " ) ; n ++ ; } for ( int i = n ; i < 8 ; i ++ ) { outputBuilder . append ( "   " ) ; } outputBuilder . append ( "    " ) ; for ( int i = p ; i < length ; i ++ ) { int b = 0xff & byteBuffer [ i ] ; if ( b > 32 && b < 127 ) { outputBuilder . append ( ( char ) b + " " ) ; } else { outputBuilder . append ( ". " ) ; } } outputBuilder . append ( "\n" ) ; return outputBuilder . toString ( ) ; }
dumps the given bytes to stdout as a hex dump ( up to length bytes ) .
