code: public static final String hexdump(byte[] buffer, int offset, int length, boolean ascii)
     {
        StringBuilder sb = new StringBuilder();
  
        if (buffer != null)
        {
           char c;
           int loop;
           int count = offset + length;
  
           for (loop = offset; loop < count; loop++)
           {
              sb.append(" ");
              sb.append(HEX_DIGITS[(buffer[loop] & 0xF0) >> 4]);
              sb.append(HEX_DIGITS[buffer[loop] & 0x0F]);
           }
  
           if (ascii == true)
           {
              sb.append("   ");
  
              for (loop = offset; loop < count; loop++)
              {
                 c = (char) buffer[loop];
  
                 if ((c > 200) || (c < 27))
                 {
                    c = ' ';
                 }
  
                 sb.append(c);
              }
           }
        }
  
        return (sb.toString());
     }
docs: This method generates a formatted version of the data contained
  in a byte array. The data is written both in hex, and as ASCII
  characters.
  
  @param buffer data to be displayed
  @param offset offset of start of data to be displayed
  @param length length of data to be displayed
  @param ascii flag indicating whether ASCII equivalent chars should also be displayed
  @return formatted string