public static byte[] hexStringToBytes(String hex){
  int l=(hex.length() + 1) / 2;
  byte[] r=new byte[l];
  int i=0;
  int j=0;
  if (hex.length() % 2 == 1) {
    r[0]=hexDigitToByte(hex.charAt(0));
    i=j=1;
  }
  while (i < l)   r[i++]=(byte)((hexDigitToByte(hex.charAt(j++)) << 4) | hexDigitToByte(hex.charAt(j++)));
  return r;
}

public static byte [ ] hexStringToBytes ( String hex ) { int l = ( hex . length ( ) + 1 ) / 2 ; byte [ ] r = new byte [ l ] ; int i = 0 ; int j = 0 ; if ( hex . length ( ) % 2 == 1 ) { r [ 0 ] = hexDigitToByte ( hex . charAt ( 0 ) ) ; i = j = 1 ; } while ( i < l ) r [ i ++ ] = ( byte ) ( ( hexDigitToByte ( hex . charAt ( j ++ ) ) << 4 ) | hexDigitToByte ( hex . charAt ( j ++ ) ) ) ; return r ; }
convert a string of hexadecimal digits to a byte array .
