title: C# copy string to byte buffer
body: <p>I am trying to copy an Ascii string to a byte array but am unable. How?</p>
  
  <hr>
  
  <p>Here are the two things I have tried so far. Neither one works:</p>
  
  <pre><code>public int GetString (ref byte[] buffer, int buflen)
  {
      string mystring = "hello world";
  
      // I have tried this:
      System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
      buffer = encoding.GetBytes(mystring);
  
      // and tried this:
      System.Buffer.BlockCopy(mystring.ToCharArray(), 0, buffer, 0, buflen);  
     return (buflen);
  }
  </code></pre>