Stack over. crc on python

212 views Asked by At

I must calculate 16b crc.

It is the data i hex format

data = "01190026032700169798001100000358057900002893"

CRC for data is this:

crc = "b83a"

This crc is calculate with these function

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

and

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

OK i try something like that. But result is different.

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)
1

There are 1 answers

0
Grigor Kolev On

done

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