title: In Java, how do I convert a hex string to a byte[]?
body: <p>I am using the below function in Java to convert an encrypted String into hex format:</p>
  
  <pre><code>public static String toHex(byte [] buf) {
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
      int i;
      for (i = 0; i &lt; buf.length; i++) {
          if (((int) buf[i] &amp; 0xff) &lt; 0x10) {
              strbuf.append("0");
          }
          strbuf.append(Long.toString((int) buf[i] &amp; 0xff, 16));
      }
      return strbuf.toString();
  }
  </code></pre>
  
  <p>Now I want to convert that hex string back into a byte array. How can I do that?</p>
  
  <p>For example,</p>
  
  <pre><code>(1) Plain Text = 123
  (2) Encrypted Text = «h&gt;kq*«¬Mí“~èåZ  \}?
  (3) Encrypted Text in Hex = f263575e7b00a977a8e9a37e08b9c215feb9bfb2f992b2b8f11e
  </code></pre>
  
  <p>I can go from <code>(2)</code> to <code>(3)</code>, but how do I go from <code>(3)</code> back to <code>(2)</code>?</p>