title: How to assign byte[] as a pointer in C#
body: <p>I have a function that generates a CRC check byte based on the content of any packet.The problem is in translating the function from C++ to C#</p>
  
  <p>C++ code:</p>
  
  <pre><code>unsigned char GenerateCheckByte( char* packet, int length, unsigned long seed )
  {
  if( !packet ) return 0;
  unsigned long checksum = 0xFFFFFFFF;
  length &amp;= 0x7FFF;
  char* ptr = packet;
  unsigned long moddedseed = seed &lt;&lt; 8;
  for( int i = 0; i &lt; length; i++ )
      checksum = ( checksum &gt;&gt; 8 ) ^ table[moddedseed + ( ( *(ptr++) ^ checksum ) &amp; 0xFF )];
  unsigned char result = ( (checksum&gt;&gt;24)&amp;0xFF ) + ( (checksum&gt;&gt;8)&amp;0xFF ) + ( (checksum&gt;&gt;16)&amp;0xFF ) + ( checksum&amp;0xFF );
  return result;
  }
  </code></pre>
  
  <p>the char*(packet) can also be defined as LPBYTE,the idea is that the value assigned to *packet is assigned to *ptr and as you see *ptr increases.Meaning a byte array is passed in and by increasing the value of the pointer it goes to the next byte.</p>
  
  <p>I tried to do it in C# and failed many times.After some hard work I figured out some code,but i can't execute it :?</p>
  
  <p>C# code</p>
  
  <pre><code>    public static unsafe byte GenerateCheckByte(byte *packet, int length, UInt32 seed )
      {
          if (*packet == 0)
          return 0;
          UInt32 checksum = 0xFFFFFFFF;
          length &amp;= 0x7FFF;
          byte *ptr = packet;
          UInt32 moddedseed = seed &lt;&lt; 8;
          for (int i = 0; i &lt; length; i++)
              checksum = ( checksum &gt;&gt; 8 ) ^ Table.table[moddedseed + ( ( *(ptr++) ^ checksum ) &amp; 0xFF )];
          byte result = (byte)(( (checksum&gt;&gt;24)&amp;0xFF ) + ( (checksum&gt;&gt;8)&amp;0xFF ) + ( (checksum&gt;&gt;16)&amp;0xFF ) + ( checksum&amp;0xFF ));
          return result;
      }
  </code></pre>
  
  <p>It doesn't look that bad,but I can't call it</p>
  
  <pre><code>  unsafe
    {
        packetBuffer[5] = Functions.GenerateCheckByte(&amp;packetBuffer[0], 18, packet.seedCRC);
    }
  </code></pre>
  
  <p>error: "You can only take the address of an unfixed expression inside of a fixed statement initializer"</p>
  
  <p><strong>Please note</strong></p>
  
  <p>packetbuffer in both C++ and C# application is byte[] packetBuffer = new byte[18];</p>