What's the 'best' way to parse a stream into a struct or class?
<p>actually i'm working with .Net Framework 3.5, so i have all these nice little features like lambdas, linq, etc.</p>

<p>Given is a serial connection (or to be more abstract: a stream) where you receive some data, which will be in a format like this:</p>

<pre><code>struct Packet
{
    byte STX
    UInt16 DataLength
    string Data
    byte CRC
    byte ETX
}
</code></pre>

<p>Using a simple mapping of the incoming data doesn't help due to the fact, that you don't really know how long one packet will be, cause it's written within the structure (the DataLength).</p>

<p>So my first idea would be to read the stream by byte and put it into ???. Yes, that's the next question. Where to store this first Raw data? Into a simple byte array, with the maximum possible length (that would be 65540 bytes, due to the fact, that DataLength is an UInt16 plus the additional bytes from the other fields). Or should i open up a Queue and fill it up will all the incoming bytes or maybe exists there another nice possibility?</p>

<p>Let's assume these problems are cleared and i have some kind of local buffer, that holds all the raw bytes from the stream. What's the nicest way to interpret it by the given structure?? Just doing some kind of for- or foreach-loop or exists there a smarter (with better performance) way (e.g. with regex or linq)?</p>

<p>Best regards,
Oliver</p>
