Calculating CRC16 in Python
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping
--
Chapters
00:00 Calculating Crc16 In Python
00:56 Accepted Answer Score 7
01:27 Answer 2 Score 7
01:59 Answer 3 Score 1
02:16 Answer 4 Score 2
02:47 Thank you
--
Full question
https://stackoverflow.com/questions/3520...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27 #crc16
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Horror Game Menu Looping
--
Chapters
00:00 Calculating Crc16 In Python
00:56 Accepted Answer Score 7
01:27 Answer 2 Score 7
01:59 Answer 3 Score 1
02:16 Answer 4 Score 2
02:47 Thank you
--
Full question
https://stackoverflow.com/questions/3520...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27 #crc16
#avk47
ACCEPTED ANSWER
Score 7
crcmod is working fine. You are not giving it the three bytes you think you are giving it. Your str(int(0x5A0001)) is providing seven bytes, which are the ASCII characters 5898241 — the conversion of 0x5a0001 to decimal.
To feed it the bytes 0x5a 0x00 0x01, you would instead (as one approach):
print hex(crc16("5a0001".decode("hex")))
That prints 0xace.
ANSWER 2
Score 7
Here is a python implementation of CRC-16/CCITT-FALSE
def crc16(data : bytearray, offset , length):
if data is None or offset < 0 or offset > len(data)- 1 and offset+length > len(data):
return 0
crc = 0xFFFF
for i in range(0, length):
crc ^= data[offset + i] << 8
for j in range(0,8):
if (crc & 0x8000) > 0:
crc =(crc << 1) ^ 0x1021
else:
crc = crc << 1
return crc & 0xFFFF
- data : bytearray of the data you want to calculate CRC for
- offset : from which offset you want to start calculating CRC
- length : to which offset you want to calculate CRC
ANSWER 3
Score 2
A working single function example for CRC-16-ANSI, CRC-16-IBM based on [pycrc](<https://pypi.org/project/pycrc/) code.
It is easy to modify but input or output reflection capability is not included:
def crc16(data: bytes):
xor_in = 0x0000 # initial value
xor_out = 0x0000 # final XOR value
poly = 0x8005 # generator polinom (normal form)
reg = xor_in
for octet in data:
# reflect in
for i in range(8):
topbit = reg & 0x8000
if octet & (0x80 >> i):
topbit ^= 0x8000
reg <<= 1
if topbit:
reg ^= poly
reg &= 0xFFFF
# reflect out
return reg ^ xor_out
ANSWER 4
Score 1
def crc16(data : bytearray, offset, length):
if data is None or offset < 0 or offset > len(data) - 1 and offset + length > len(data):
return 0
print("uzunluk=", len(data))
print(data)
crc = 0xFFFF
for i in range(length):
crc ^= data[offset + i]
for j in range(8):
print(crc)
if ((crc & 0x1) == 1):
print("bb1=", crc)
crc = int((crc / 2)) ^ 40961
print("bb2=", crc)
else:
crc = int(crc / 2)
return crc & 0xFFFF