Meloun
Meloun

Reputation: 15049

CRC in python, little Endian

I need to calc CRC checksumme of binary file. This file content CRC too and by comparing I find out when file was corrupted.

Bin file is something like long hex string

00200020 595A0008 ......

But CRC in file was calculated per integer(4.byte little Endian) like this

1.int -  0x20002000
2.int -  0x8000A559

How can I get the same result without switching bytes in python? I was trying http://www.tty1.net/pycrc/ and played with reflect in, but I dont get the same result.

For this two bytes is correct crc 0xEF2B32F8

Upvotes: 0

Views: 1991

Answers (2)

Shakib
Shakib

Reputation: 11

I have written the following code for calculating crc8:

acklist = [] # a list of your byte string data
x = 0xff
    crc = 0
    for i in range(len(acklist)):
        crc += int(acklist[i], 16) & x

print(crc)

crc = ~crc
crc += 1

crc1 = crc >> 8 & x
crc2 = crc & x

Upvotes: 0

waffleman
waffleman

Reputation: 4339

Try using the struct module. You can open a file and use the unpack read the data in any format you want with any Endianess.

Upvotes: 1

Related Questions