title: Comparing two hex values as strings
body: <p>I'm writing a program that creates random strings and then hashes the string to get its MAC. I want to then look at the first byte for the hash and see if is equal to a specific hex value. (Pre-image attack simplified.) My code successfully pulls the first byte off of each hash but doesn't compare it correctly. So even if the the two bytes are equal, the while loop doesn't recognize it and keeps going indefinitely.</p>
  
  <pre><code>    Random generator = new Random();
      Boolean found = false;
      int i;
      String test="";
      int whatIWant = 169;
  
  
      while(found == false)
      {
  
          String x = "";
          i = 0;
  
      while(i&lt;15000)
      {   //String x = "";
  
  
          int y = generator.nextInt(220)+20;
          x = x + Integer.toHexString(y);
          i++;
      }
      byte[] hexMessage = DatatypeConverter.parseHexBinary(x);
      MessageDigest cript = MessageDigest.getInstance("SHA-512");
      cript.reset();
      cript.update(hexMessage);
      byte[] hash = cript.digest();
  
      test = String.format("%02X ", hash[0]);
  
      if(test.equalsIgnoreCase(Integer.toHexString(whatIWant).toString()))
          found = true;
  </code></pre>