public static byte[] hexString2ByteArray(String hex) throws IllegalArgumentException {
  if (hex.length() % 2 != 0)   hex='0' + hex;
  char[] chars=hex.toCharArray();
  byte[] bytes=new byte[chars.length / 2];
  int byteCnt=0;
  for (int i=0; i < chars.length; i+=2) {
    int newByte=0;
    newByte|=hexCharToByte(chars[i]);
    newByte<<=4;
    newByte|=hexCharToByte(chars[i + 1]);
    bytes[byteCnt]=(byte)newByte;
    byteCnt++;
  }
  return bytes;
}

public static byte [ ] hexString2ByteArray ( String hex ) throws IllegalArgumentException { if ( hex . length ( ) % 2 != 0 ) hex = '0' + hex ; char [ ] chars = hex . toCharArray ( ) ; byte [ ] bytes = new byte [ chars . length / 2 ] ; int byteCnt = 0 ; for ( int i = 0 ; i < chars . length ; i += 2 ) { int newByte = 0 ; newByte |= hexCharToByte ( chars [ i ] ) ; newByte <<= 4 ; newByte |= hexCharToByte ( chars [ i + 1 ] ) ; bytes [ byteCnt ] = ( byte ) newByte ; byteCnt ++ ; } return bytes ; }
convert string with hex values to byte array .
