title: In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?
body: <p>I'm working with some example java code for making md5 hashes. One part converts the results from bytes to a string of hex digits:</p>
  
  <pre><code>byte messageDigest[] = algorithm.digest();     
  StringBuffer hexString = new StringBuffer();
  for (int i=0;i&lt;messageDigest.length;i++) {
      hexString.append(Integer.toHexString(0xFF &amp; messageDigest[i]));
      }
  </code></pre>
  
  <p>However, it doesn't quite work since toHexString apparently drops off leading zeros. So, what's the simplest way to go from byte array to hex string that maintains the leading zeros?</p>