Converting C++ to C# - code to work with MPEG-TS (Transport Stream)
<p><em>I'M NOT ASKING FOR HELP WITH DEALING WITH PACKETS NOR FOR SOMEONE TO CONVERT FOR ME</em></p>

<p>I need to rewrite some C++ code in C# but am not familiar enough with C# to figure out what to use in place of everything in the original code.</p>

<p>Here's just 1 function from the C++ (it uses a class (BitStream), but I'm not including this here).</p>

<p>I'm not really sure what specific question(s) to ask but any help with this conversion would be greatly appreciated!!</p>

<pre><code>int ParseTxPkt(BYTE *pTxPkt, BYTE **ppPayload, int &amp;payloadLen, int &amp;pid, int &amp;payload_unit_start_indicator,int &amp;continuity_counter,__int64 &amp;pcr,__int64 &amp;offset,bool peek)
{
  int transport_error_indicator;
  int transport_priority;
  int transport_scrambling_control;
  int adaptation_field_control;
  int adaptation_field_length;
  int payloadOffset;
  bool pcr_flag;
  int ret=QV_FAILURE;
  BYTE *txPkt;

  BitStream *stream=NULL;  
  payloadOffset=-1;
  payloadLen=0;
  *ppPayload=NULL;
  pcr=-1;
  pcr_flag=false;

  txPkt=pTxPkt;

  stream=new BitStream(txPkt,TX_PKT_SIZE);

  if (stream-&gt;GetNextBits(8)!=Sync_Byte) {
    delete stream;
    return QV_FAILURE;
  }

  transport_error_indicator=stream-&gt;GetNextBit();
  payload_unit_start_indicator=stream-&gt;GetNextBit();
  transport_priority=stream-&gt;GetNextBit();
  pid=stream-&gt;GetNextBits(13);
  transport_scrambling_control=stream-&gt;GetNextBits(2);
  adaptation_field_control=stream-&gt;GetNextBits(2);
  continuity_counter=stream-&gt;GetNextBits(4);

  switch (adaptation_field_control) {
  case 0:    // reserved
    break;
  case 2:    // no payload
    *ppPayload=NULL;
    payloadLen=0;
    adaptation_field_length=stream-&gt;GetNextBits(8);
    if (adaptation_field_length&gt;0 &amp;&amp; pcr&lt;0) {
      stream-&gt;GetNextBits(3);
      pcr_flag=stream-&gt;GetNextBits(1);
      stream-&gt;GetNextBits(4);
      if (pcr_flag) {
        pcr=stream-&gt;GetNextBits(32);
        pcr=pcr*2+stream-&gt;GetNextBits(1);
      }
    }
    ret=QV_SUCCESS;
    break;
  case 1:    // no adaptation field
    payloadOffset=4;
    payloadLen=TX_PKT_SIZE-4;
    *ppPayload=txPkt+payloadOffset;
    ret=QV_SUCCESS;
    break;
  case 3:    // adaptation field and payload
    adaptation_field_length=stream-&gt;GetNextBits(8);
    payloadOffset=4+1+adaptation_field_length;
    payloadLen=TX_PKT_SIZE-4-(1+adaptation_field_length);
    *ppPayload=txPkt+payloadOffset;
    if (adaptation_field_length&gt;0) {
      stream-&gt;GetNextBits(3);
      pcr_flag=stream-&gt;GetNextBits(1);
      stream-&gt;GetNextBits(4);
      if (pcr_flag) {
        pcr=stream-&gt;GetNextBits(32);      
        pcr=pcr*2+stream-&gt;GetNextBits(1);
      }
    }
    ret=QV_SUCCESS;
    break;
  }

  delete stream;
  return ret;
}
</code></pre>
