static byte[] hexToBytes(String hex){
  if (hex == null) {
    return EMPTY_BYTES;
  }
  hex=hex.replaceAll("\\s+","");
  if (hex.length() == 0) {
    return EMPTY_BYTES;
  }
  ByteArrayOutputStream baos=new ByteArrayOutputStream(hex.length() / 2);
  byte b;
  for (int i=0; i < hex.length(); i++) {
    int high=charToNibble(hex.charAt(i));
    int low=0;
    if (i < hex.length() - 1) {
      i++;
      low=charToNibble(hex.charAt(i));
    }
    b=(byte)(high << 4 | low);
    baos.write(b);
  }
  return baos.toByteArray();
}

static byte [ ] hexToBytes ( String hex ) { if ( hex == null ) { return EMPTY_BYTES ; } hex = hex . replaceAll ( "\\s+" , "" ) ; if ( hex . length ( ) == 0 ) { return EMPTY_BYTES ; } ByteArrayOutputStream baos = new ByteArrayOutputStream ( hex . length ( ) / 2 ) ; byte b ; for ( int i = 0 ; i < hex . length ( ) ; i ++ ) { int high = charToNibble ( hex . charAt ( i ) ) ; int low = 0 ; if ( i < hex . length ( ) - 1 ) { i ++ ; low = charToNibble ( hex . charAt ( i ) ) ; } b = ( byte ) ( high << 4 | low ) ; baos . write ( b ) ; } return baos . toByteArray ( ) ; }
utility method to convert a hex string to a byte array .
