Note that there are some explanatory texts on larger screens.

plurals
  1. POPort C++ i_pwCalculateCRC16Continuation to C#
    text
    copied!<p>How to correctly convert the following C++ into C#?</p> <pre><code>//-------------------------------------------------------------------- // Calculate a 16-bit Cycle Redundency Check (CRC) value on a block of //data. // // Params: // pData : Pointer to data to calculate CRC. // dwSize : Size of data in bytes. // // Return: // 16-bit CRC value. // // Notes: //-------------------------------------------------------------------- private WORD i_pwCalculateCRC16Continuation(PBYTE pData, WORD dwSize, WORD wCRC) { // high byte of CRC initialized BYTE cCRCHi = (BYTE) ((wCRC &gt;&gt; 8) &amp; 0xFF); // low byte of CRC initialized BYTE cCRCLo = (BYTE) (wCRC &amp; 0xFF); // will index into CRC lookup table BYTE cIndex; while (dwSize--) // step through each byte of data { cIndex = cCRCHi ^ *pData++; // calculate the CRC cCRCHi = cCRCLo ^ m_cCRCHiArray[cIndex]; cCRCLo = m_cCRCLoArray[cIndex]; } return (cCRCHi &lt;&lt; 8) + cCRCLo; } </code></pre> <p>I found a tool that converted it but it were not perfect and it does not understand the context.</p> <pre><code>private ushort i_pwCalculateCRC16Continuation(ref byte pData, ushort dwSize, ushort wCRC) { byte cCRCHi = (byte) ((wCRC &gt;&gt; 8) &amp; 0xFF); byte cCRCLo = (byte) (wCRC &amp; 0xFF); byte cIndex; while (dwSize-- &gt; 0) { // Cannot convert source type 'int' to target type 'byte' cIndex = cCRCHi ^ pData++; // Cannot apply indexing to an expression of type 'byte' cCRCHi = cCRCLo ^ m_cCRCHiArray[cIndex]; // Cannot apply indexing to an expression of type 'byte' cCRCLo = m_cCRCLoArray[cIndex]; } // Cannot convert expression type 'int' to return type 'ushort' return (cCRCHi &lt;&lt; 8) + cCRCLo; } </code></pre> <p>The CRC concept and bit wise operations are little bit unfamiliar for me and I do not understand the above code well. Hence I do not know how to "fix" it.</p> <p>Thanks</p> <p><strong>EDIT:</strong> Missed the following variables.</p> <pre><code>private static byte[] m_cCRCHiArray = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x81, 0x40 }; private static byte[] m_cCRCLoArray = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0x80, 0x40 }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload