void CRC_calcCrc8(u16 *crcReg, u16 poly, u16 u8Data)
{
u16 i;
u16 xorFlag;
u16 bit;
u16 dcdBitMask = 0x80;
for(i=0; i<8; i++)
{
// Get the carry bit. This determines if the polynomial should be
// xor'd with the CRC register.
xorFlag = *crcReg & 0x8000;
// Shift the bits over by one.
*crcReg <<= 1;
// Shift in the next bit in the data byte
bit = ((u8Data & dcdBitMask) == dcdBitMask);
*crcReg |= bit;
// XOR the polynomial
if(xorFlag)
{
*crcReg = *crcReg ^ poly;
}
// Shift over the dcd mask
dcdBitMask >>= 1;
}
}
翻译为C#代码 谢了 有计算crc好多方法也可以呢
void CRC_calcCrc8(ref ushort crcReg, ushort poly, ushort u8Data)
{
ushort i;
ushort xorFlag;
ushort bit;
ushort dcdBitMask = 0x80;
for(i=0; i<8; i++)
{
// Get the carry bit. This determines if the polynomial should be
// xor'd with the CRC register.
xorFlag = crcReg & 0x8000;
// Shift the bits over by one.
crcReg <<= 1;
// Shift in the next bit in the data byte
bit = ((u8Data & dcdBitMask) == dcdBitMask);
crcReg |= bit;
// XOR the polynomial
if(xorFlag)
{
crcReg = crcReg ^ poly;
}
// Shift over the dcd mask
dcdBitMask >>= 1;
}
}
我也是这么翻译过来的,但是会报错呢。bit = ((u8Data & dcdBitMask) == dcdBitMask);这句
@NET技术员: bit = ((u8Data & dcdBitMask) == dcdBitMask) ? 1:0;
麻烦你下次说清楚是编译出错,还是运行时出错。