首页 新闻 赞助 找找看

CCITT CRC-16 计算

0
悬赏园豆:10 [已解决问题] 解决于 2013-03-21 16:52

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好多方法也可以呢

C#
NET技术员的主页 NET技术员 | 初学一级 | 园豆:8
提问于:2013-01-15 16:43
< >
分享
最佳答案
0

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

收获园豆:10
Launcher | 高人七级 |园豆:45045 | 2013-01-15 17:03

我也是这么翻译过来的,但是会报错呢。bit = ((u8Data & dcdBitMask) == dcdBitMask);这句

NET技术员 | 园豆:8 (初学一级) | 2013-01-15 17:05

@NET技术员: bit = ((u8Data & dcdBitMask) == dcdBitMask) ? 1:0;

麻烦你下次说清楚是编译出错,还是运行时出错。

Launcher | 园豆:45045 (高人七级) | 2013-01-15 17:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册