public static byte[] toBytes(String hex){
  if (hex == null)   return null;
  int len=hex.length();
  byte[] bytes=new byte[len / 2];
  int k=0;
  for (int i=0; i < len; i+=2) {
    int digit=0;
    char ch=hex.charAt(i);
    if ('0' <= ch && ch <= '9')     digit=ch - '0';
 else     if ('a' <= ch && ch <= 'f')     digit=ch - 'a' + 10;
 else     if ('A' <= ch && ch <= 'F')     digit=ch - 'A' + 10;
    ch=hex.charAt(i + 1);
    if ('0' <= ch && ch <= '9')     digit=16 * digit + ch - '0';
 else     if ('a' <= ch && ch <= 'f')     digit=16 * digit + ch - 'a' + 10;
 else     if ('A' <= ch && ch <= 'F')     digit=16 * digit + ch - 'A' + 10;
    bytes[k++]=(byte)digit;
  }
  return bytes;
}

public static byte [ ] toBytes ( String hex ) { if ( hex == null ) return null ; int len = hex . length ( ) ; byte [ ] bytes = new byte [ len / 2 ] ; int k = 0 ; for ( int i = 0 ; i < len ; i += 2 ) { int digit = 0 ; char ch = hex . charAt ( i ) ; if ( '0' <= ch && ch <= '9' ) digit = ch - '0' ; else if ( 'a' <= ch && ch <= 'f' ) digit = ch - 'a' + 10 ; else if ( 'A' <= ch && ch <= 'F' ) digit = ch - 'A' + 10 ; ch = hex . charAt ( i + 1 ) ; if ( '0' <= ch && ch <= '9' ) digit = 16 * digit + ch - '0' ; else if ( 'a' <= ch && ch <= 'f' ) digit = 16 * digit + ch - 'a' + 10 ; else if ( 'A' <= ch && ch <= 'F' ) digit = 16 * digit + ch - 'A' + 10 ; bytes [ k ++ ] = ( byte ) digit ; } return bytes ; }
convert hex to bytes
