title: What is the best way to convert a hexidecimal string to a byte array (.NET)?
body: <p>I have a hexidecimal string that I need to convert to a byte array. The best way (ie efficient and least code) is:</p>
  
  <pre><code>string hexstr = "683A2134";
  byte[] bytes = new byte[hexstr.Length/2];
  for(int x = 0; x &lt; bytes.Length; x++)
  {
      bytes[x] = Convert.ToByte(hexstr.Substring(x * 2, 2), 16);
  }
  </code></pre>
  
  <p>In the case where I have a 32bit value I can do the following:</p>
  
  <pre><code>string hexstr = "683A2134";
  byte[] bytes = BitConverter.GetBytes(Convert.ToInt32(hexstr, 16)); 
  </code></pre>
  
  <p>However what about in the general case? Is there a better built in function, or a clearer (doesn't have to be faster, but still performant) way of doing this?</p>
  
  <p>I would prefer a built in function as there seems to be one for everything (well common things) except this particular conversion.</p>