title: Need a little more help converting a CRC function from VB.NET to C#
body: <p>I probably should have just put all this in one question, sorry for that :(</p>
  <h1>Second error</h1>
  <p>Ok, this should be the end of errors (hopefully):</p>
  <pre><code>private static string getCRC(string input, bool appendDate)
  {
      string toEnc = string.Format(&quot;{0}{1}&quot;, input, appendDate == true ? string.Format(&quot;{0:yyyyMMdd}&quot;, System.DateTime.Now) : &quot;&quot;);
      System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(toEnc), false);
      
      CRC32 oCRC = new CRC32();
      return oCRC.GetCrc32(((System.IO.Stream)mStream)).ToString();
  }
  </code></pre>
  <p>Error is on the return line:</p>
  <blockquote>
  <p><strong>Compiler Error Message:</strong> CS1502: The best overloaded method match for 'CRC.CRC32.GetCrc32(ref System.IO.Stream)' has some invalid arguments</p>
  </blockquote>
  <p>Here is the function it is referring to:</p>
  <pre><code>public UInt32 GetCrc32(ref System.IO.Stream stream)
  {
      //Dim crc32Result As Integer = &amp;HFFFFFFFF
      UInt32 crc32Result = UInt32.MaxValue;
  
      try
      {
  
          byte[] buffer = new byte[BUFFER_SIZE];
          int readSize = BUFFER_SIZE;
  
          int count = stream.Read(buffer, 0, readSize);
          int i;
          UInt32 iLookup;
          //Dim tot As Integer = 0
          while ((count &gt; 0))
          {
              for (i = 0; i &lt;= count - 1; i++)
              {
                  iLookup = (crc32Result &amp; 0xff) ^ buffer[i];
                  //crc32Result = ((crc32Result And &amp;HFFFFFF00) \ &amp;H100) And &amp;HFFFFFF                     ' nasty shr 8 with vb :/
                  crc32Result = ((UInt32)((crc32Result &amp; 4294967040L) / 256) &amp; UInt32.MaxValue);
                  // nasty shr 8 with vb :/
                  crc32Result = crc32Result ^ crc32Table[iLookup];
              }
              count = stream.Read(buffer, 0, readSize);
          }
      }
      catch (Exception ex)
      {
          HttpContext.Current.Response.Output.Write(string.Format(&quot;{0}{1}{2}&quot;, ex.Message, Environment.NewLine, ex.StackTrace));
          System.Diagnostics.Debugger.Break();
      }
  
      return (~crc32Result);
  
  }
  </code></pre>
  <p>As you can see I tried to cast the memorystream as a stream, but I don't think it liked that too much.</p>
  <h1>First error</h1>
  <p>(<a href="https://stackoverflow.com/questions/1179682/need-a-little-more-help-converting-a-crc-function-from-vb-net-to-c/1179695#1179695">solved here</a>)</p>
  <p>If you have not seen my last couple of questions, I have a CRC function written in VB.NET. I used an online converter to convert it over to C#. Here is the original VB code:</p>
  <pre><code>Public Sub New()
      Dim dwPolynomial As UInt32 = 3988292384
      Dim i As Integer, j As Integer
  
      ReDim crc32Table(256)
      Dim dwCrc As UInt32
  
      For i = 0 To 255
          dwCrc = i
          For j = 8 To 1 Step -1
              If (dwCrc And 1) Then
                  dwCrc = ((dwCrc And &amp;HFFFFFFFE) \ 2&amp;) And &amp;H7FFFFFFF
                  dwCrc = dwCrc Xor dwPolynomial
              Else
                  dwCrc = ((dwCrc And &amp;HFFFFFFFE) \ 2&amp;) And &amp;H7FFFFFFF
              End If
          Next j
          crc32Table(i) = dwCrc
      Next i
  End Sub
  </code></pre>
  <p>Here is my converted code. I fixed a few of the errors I was having but there are few more that I cannot figure out:</p>
  <pre><code>public CRC32()
  {
      UInt32 dwPolynomial = ((UInt32)3988292384L);
      int i;
      int j;
  
      UInt32[] crc32Table;
      crc32Table = new UInt32[256];
      UInt32 dwCrc;
  
      for (i = 0; i &lt;= 255; i++)
      {
          dwCrc = ((UInt32)i);
          for (j = 8; j &gt;= 1; j -= 1)
          {
              if ((dwCrc &amp; 1))
              {
                  dwCrc = ((dwCrc &amp; 0xfffffffe) / 2L) &amp; 0x7fffffff;
                  dwCrc = dwCrc ^ dwPolynomial;
              }
              else
              {
                  dwCrc = ((dwCrc &amp; 0xfffffffe) / 2L) &amp; 0x7fffffff;
              }
          }
          crc32Table[i] = dwCrc;
      }
  }
  </code></pre>
  <p>The first error is on this line:</p>
  <blockquote>
  <p><strong>Compiler Error Message:</strong> CS0030: Cannot convert type 'uint' to 'bool'</p>
  <p><code>if ((dwCrc &amp; 1))</code></p>
  </blockquote>
  <p>Should I be comparing these two values with a different operator? I am not too sure about why the <code>&amp;</code> operator is there to be honest.</p>
  <p>Thanks SO.</p>