title: How to GetBytes from string appropriately?
body: <p>I have a string variable from which I get the following bytes with the following loop:</p>
  
  <pre><code>Bytes I get: 1e 05 55 3c *e2 *91 6f 03 *fe 1a 1d *f4 51 6a 5e 3a *ce *d1 04 *8c 
  
  With that loop:
  
    byte[] temp = new byte[source.Length];
    string x = "";
    for (int i = 0;i != source.Length;i++)
    {
      temp[i] = ((byte) source[i]);
    }
  </code></pre>
  
  <p>Now I have wanted to simplify that operation and use Encoding's GetBytes.
  The problem is I cannot fit an appropriate encoding. e.g. I get several bytes incorrect:</p>
  
  <pre><code>Encoding.ASCII.GetBytes(source):    1e 05 55 3c *3f *3f 6f 03 *3f 1a 1d *3f 51 6a 5e 3a *3f *3f 04 *3f
  Encoding.Default.GetBytes(source):  1e 05 55 3c  e2  3f 6f 03  3f 1a 1d  f4 51 6a 5e 3a  ce  4e 04  3f
  </code></pre>
  
  <p>How can I get rid of that loop and use Encoding's GetBytes?</p>
  
  <p>Here is the summary:</p>
  
  <pre><code>Loop(correct bytes):                1e 05 55 3c *e2 *91 6f 03 *fe 1a 1d *f4 51 6a 5e 3a *ce *d1 04 *8c 
  
  Encoding.ASCII.GetBytes(source):    1e 05 55 3c *3f *3f 6f 03 *3f 1a 1d *3f 51 6a 5e 3a *3f *3f 04 *3f
  Encoding.Default.GetBytes(source):  1e 05 55 3c  e2  3f 6f 03  3f 1a 1d  f4 51 6a 5e 3a  ce  4e 04  3f
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>Addition:</p>
  
  <p>I have a string input in hex, sth like: "B1807869C20CC1788018690341"
  then I transfer this into string with the method:</p>
  
  <pre><code>private static string hexToString(string sText)
  {
    int i = 0;
    string plain = "";
    while (i &lt; sText.Length)
    {
      plain += Convert.ToChar(Convert.ToInt32(sText.Substring(i, 2), 16));
      i += 2;
    }
    return plain;
  }
  </code></pre>