我必须计算16b crc。
这是十六进制格式的数据
data = "01190026032700169798001100000358057900002893"
数据的CRC是这样的:
crc = "b83a"
这个crc是用这些函数计算的
unsigned short CRC(unsigned char *s, int len, unsigned
short crcval)
{
register unsigned c,q;
for (; len; len--)
{
c = *s++;
q = (crcval ^ c) & 017;
crcval = (crcval >> 4) ^ (q * 010201);
q = (crcval ^ (c >> 4)) & 017;
crcval = (crcval >> 4) ^ (q * 010201);
}
return (crcval);
}
和
uint16_t sas_crc_add_byte(uint16_t crc, uint8_t b)
{
uint16_t q = (crc ^ b) & 017;
crc = (crc >> 4) ^ (q * 010201);
q = (crc ^ (b >> 4)) & 017;
crc = (crc >> 4) ^ (q * 010201);
return crc;
}
好的,我尝试类似的东西。但结果不同。
def crc(b, seed=0):
for x in b:
q = (seed ^ int(x, 16)) & 0o17
seed = (seed >> 4) ^ (q * 0o10201)
q = (seed ^ (int(x, 16) >> 4)) & 0o17
seed = (seed >> 4) ^ (q * 0o10201)
return hex(seed)
答案 0 :(得分:0)
完成
def crc(b, seed=0):
c = ''
for x in b:
c = c + x
if len(c) == 2:
q = (seed ^ int(c, 16)) & 0o17
seed = (seed >> 4) ^ (q * 0o010201)
q = (seed ^ (int(c, 16) >> 4)) & 0o17
seed = (seed >> 4) ^ (q * 0o010201)
c=''
data = hex(seed)
data = data[4:] + data[2:-2]
return data