public static byte[] hexToByteArray(String hex) {
    int length = hex.length();
    byte[] result = new byte[(length / 2) + (length % 2)];
    for (int i = length, j = result.length - 1; i > 0; i -= 2, j--) {
      result[j] = hexToByte(hex.charAt(i - 1));
      if (i > 1) {
        result[j] |= (hexToByte(hex.charAt(i - 2)) << 4);
      }
    }
    return result;
  }
public static byte [ ] hexToByteArray ( String hex ) { int length = hex . length ( ) ; byte [ ] result = new byte [ ( length / 2 ) + ( length % 2 ) ] ; for ( int i = length , j = result . length - 1 ; i > 0 ; i -= 2 , j -- ) { result [ j ] = hexToByte ( hex . charAt ( i - 1 ) ) ; if ( i > 1 ) { result [ j ] |= ( hexToByte ( hex . charAt ( i - 2 ) ) << 4 ) ; } } return result ; }
Converts a number expressed in hexadecimal to a byte array.
@param hex The <code>String</code> representation of the number, in
hexadecimal.
@return The byte array represented by <code>hex</code>.