title: Converting std::string or string^ to byte array in c++/cli
body: <p>I know this is a frequently asked  question and I havent got a clear answer for converting a std::string or String^ to a byte array for writing in to a stream for tcp communication.</p>
  
  <p>This is what I have tried</p>
  
  <pre><code>bool CTcpCommunication::WriteBytes(const std::string&amp; rdatastr)
  {
    bool retVal = false;
  
    try
    {
      if (static_cast&lt;NetworkStream^&gt;(stream) != nullptr)
      {
        array&lt;Byte&gt;^data = System::Text::Encoding::ASCII-&gt;GetBytes(rdatastr); 
        stream-&gt;Write( data, 0, data-&gt;Length ); 
      }
    }
    catch(Exception^)
    {
      // Ignore, just return false
    }
    return retVal;
  }
  </code></pre>
  
  <p>I know that here the GetBytes wont work and I have also checked marshalling options to convert std:string to .NET String but havent found out any.Can someone help me in solving this..</p>