title: Difference between using Encoding.GetBytes or cast to byte
body: <p>I was wondering if there's any difference between converting characters to byte with <code>Encoding.UTF8.GetBytes</code> or manually using <code>(byte)</code> before characters and convert them to byte?</p>
  
  <p>For an example, look at following code:</p>
  
  <pre><code>public static byte[] ConvertStringToByteArray(string str)
  {
      int i, n;
      n = str.Length;
      byte[] x = new byte[n];
      for (i = 0; i &lt; n; i++)
      {
          x[i] = (byte)str[i];
      }
      return x;
  }
  
  var arrBytes = ConvertStringToByteArray("Hello world");
  </code></pre>
  
  <p>or </p>
  
  <pre><code>var arrBytes = Encoding.UTF8.GetBytes("Hello world");
  </code></pre>