首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >端口C++ i_pwCalculateCRC16Continuation到C#

端口C++ i_pwCalculateCRC16Continuation到C#
EN

Stack Overflow用户
提问于 2012-06-15 07:50:01
回答 1查看 114关注 0票数 0

如何正确地将以下C++转换为C#?

代码语言:javascript
复制
//--------------------------------------------------------------------
// 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;
}

我找到了一个转换它的工具,但是它并不完美,也不理解上下文。

代码语言:javascript
复制
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概念和位智能操作对我来说有点陌生,我对上面的代码不太了解。因此,我不知道如何“修复”它。

谢谢

编辑:遗漏了以下变量。

代码语言:javascript
复制
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
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-15 08:33:46

试试这个:

代码语言:javascript
复制
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);
}

乍一看,这并不是明显的错误;-)

除了将pData改为data之外,我没有修改任何匈牙利符号

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11046638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档