2012-06-15 43 views
0

如何正確地將以下C++轉換爲C#?端口C++ i_pwCalculateCRC16繼續C#

//-------------------------------------------------------------------- 
// 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 >> 8) & 0xFF); 
    // low byte of CRC initialized 
    BYTE cCRCLo = (BYTE) (wCRC & 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 << 8) + cCRCLo; 
} 

我發現了一個轉換它的工具,但它並不完美,它不理解上下文。

private ushort i_pwCalculateCRC16Continuation(ref byte pData, ushort dwSize, ushort wCRC) 
{ 
    byte cCRCHi = (byte) ((wCRC >> 8) & 0xFF); 
    byte cCRCLo = (byte) (wCRC & 0xFF); 
    byte cIndex; 

    while (dwSize-- > 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 << 8) + cCRCLo; 
} 

對於我來說CRC概念和位操作對我來說有點陌生,我不太瞭解上面的代碼。因此我不知道如何「修復」它。

感謝

編輯: 錯過以下變量。

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 
}; 
+0

雖然我不能評論一般問題(我懶得運行你的代碼!)最明顯的問題是,C++代碼中的'pData'是一個緩衝區,而不僅僅是一個指向唱歌的指針le字節值。緩衝區的長度大概存儲在dwSize中。您的C#代碼應該採用a)byte [] data數組,或b)Stream數據流,int countBytes。你的成員變量'm_cCRCHiArray'也需要是一個字節數組。 – Rook

+0

如果pData是一個字節數組,pData增量(pData ++)意味着什麼?謝謝你的評論。 –

+1

在C和C++下,您可以通過反覆增加一個指向它的指針來遍歷C風格的數組。這在C#下無效。如果'pData'是一個C#數組,你的循環看起來更像'for(int i = 0; i Rook

回答

1

試試這個:

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 
}; 

private ushort i_pwCalculateCRC16Continuation(byte[] data, ushort wCRC) 
{ 
    byte cCRCHi = (byte)((wCRC >> 8) & 0xFF); 
    byte cCRCLo = (byte)(wCRC & 0xFF); 

    byte cIndex; 

    for (int i=0; i < data.Length; i++) 
    { 
     cIndex = (byte)(cCRCHi^data[i]); 
     cCRCHi = (byte)(cCRCLo^m_cCRCHiArray[cIndex]); 
     cCRCLo = m_cCRCLoArray[cIndex]; 
    } 

    return (byte)((cCRCHi << 8) + cCRCLo); 
} 

這不是明顯的錯誤乍一看;-)

我不會改變任何匈牙利命名法中,除了改變pDatadata

+0

語法正確。現在我需要一些時間來設置測試用例,看看它產生的實際結果是否正確。謝謝! –

+0

我希望我知道什麼是wCRC參數,值是多少。我知道我需要獲得CRC低和高值。 –

+2

如果你看[本pdf]的最後一頁(http://www.clarityvisual.com/partners/common/docs/techresources/Bobcat/070-0108-01_Bobcat_I_RS232.pdf),你會看到他們開始他們的用0xFFFF進行CRC計算。這看起來和你使用的代碼是一樣的,所以這也許是一個明智的選擇。 – Rook