public static String toHexString(byte [] byteSource,int bytes)
  {
    StringBuffer result = null;
    boolean truncated = false;

    if (byteSource != null)
    {
      if (bytes > byteSource.length)
      {
        // If the number of bytes to display is larger than the available number of
        // bytes, then reset the number of bytes to display to be the available
        // number of bytes.
        bytes = byteSource.length;
      }
      else if (bytes < byteSource.length)
      {
        // If we are displaying less bytes than are available then detect this
        // 'truncation' condition.
        truncated = true;
      }

      result = new StringBuffer(bytes*2);
      for (int i = 0; i < bytes; i++)
      {
        result.append(_digits.charAt((byteSource[i] >> 4) & 0xf));
        result.append(_digits.charAt(byteSource[i] & 0xf));
      }

      if (truncated)
      {
        result.append("... (" + bytes + "/" + byteSource.length + ")");
      }
      else
      {
        result.append("(" + bytes + ")");
      }
    }
    else
    {
      result = new StringBuffer("null");
    }

    return(result.toString());
  }
public static String toHexString ( byte [ ] byteSource , int bytes ) { StringBuffer result = null ; boolean truncated = false ; if ( byteSource != null ) { if ( bytes > byteSource . length ) { bytes = byteSource . length ; } else if ( bytes < byteSource . length ) { truncated = true ; } result = new StringBuffer ( bytes * 2 ) ; for ( int i = 0 ; i < bytes ; i ++ ) { result . append ( _digits . charAt ( ( byteSource [ i ] > > 4 ) & 0xf ) ) ; result . append ( _digits . charAt ( byteSource [ i ] & 0xf ) ) ; } if ( truncated ) { result . append ( "... (" + bytes + "/" + byteSource . length + ")" ) ; } else { result . append ( "(" + bytes + ")" ) ; } } else { result = new StringBuffer ( "null" ) ; } return ( result . toString ( ) ) ; }
Converts a byte array into a printable hex string.

@param byteSource The byte array source.
@param bytes The number of bytes to display.

@return String printable hex string or "null"