title: C# how convert a string hex value in "bin" like xxd
body: <p>I'm try a solution for convert a string hex value in format &quot;binary&quot; (.bin),
  like xxd command.</p>
  <p>Example :
  I have a string hexValue &quot;17c7dfef853adcddb2c4b71dd8d0b3e3363636363&quot;</p>
  <p>On linux, i want a result like next :</p>
  <p>echo &quot;hexvalue&quot; &gt; file.hex
  cat file.hex | xxd -r -p &gt; file.bin</p>
  <p>The hexdump of file.bin give :</p>
  <blockquote>
  <p>0000000 17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3 e3</p>
  <p>0000010 36 36 36 36 36 36 36</p>
  </blockquote>
  <p>My Program for convert this, is :</p>
  <pre><code>Private static string convertHex(string value)
  {
      string[] hexVal = value.Split(' ');
      StringBuilder s = new StringBuilder();
  
      foreach (string hex in hexVal)
      {
           int val = Convert.ToInt32(hex, 16);
           string stringValue = char.ConvertFromUtf32(val);
           s.Append(stringValue);
      }
      return s.ToString();
  }
  
  string MyString = &quot;17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3 e3 36 36 36 36 36&quot;;
  string newString = converthex(MyString);
  Console.WriteLine(newString);
  File.WriteAllText(&quot;file2.bin&quot;, newString);
  </code></pre>
  <p>So now, when look the hexdump of file2.bin, i see :</p>
  <blockquote>
  <p>0000000 17 c3 87 c3 97 c3 af c2 85 3a c3 9c c3 9d c2 b2</p>
  <p>0000010 c3 84 c2 b7 1d c3 98 c3 90 c2 b3 a3 36 36 36</p>
  </blockquote>
  <p>Why c3 or c2 exist in my new file ?
  Have you a solution for me ?</p>
  <p>Thank for your help !</p>