Last active
June 12, 2024 13:30
-
-
Save oysstu/68072c44c02879a2abf94ef350d1c7c6 to your computer and use it in GitHub Desktop.
Implementation of crc16 (CRC-16-CCITT) in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crc16(data: bytes, poly=0x8408): | |
''' | |
CRC-16-CCITT Algorithm | |
''' | |
data = bytearray(data) | |
crc = 0xFFFF | |
for b in data: | |
cur_byte = 0xFF & b | |
for _ in range(0, 8): | |
if (crc & 0x0001) ^ (cur_byte & 0x0001): | |
crc = (crc >> 1) ^ poly | |
else: | |
crc >>= 1 | |
cur_byte >>= 1 | |
crc = (~crc & 0xFFFF) | |
crc = (crc << 8) | ((crc >> 8) & 0xFF) | |
return crc & 0xFFFF |
kjm1102
commented
Apr 27, 2023
via email
fromhex won't work if you're using upython? You could try
import binascii
bits_string = ''.join(str(bit) for bit in input_list)
int_value = int(bits_string, 2)
hex = hex(int_value)[2:]
hex_value = binascii.unhexlify(hex)
…On Thu, 27 Apr 2023 at 19:03, TrajcheKrstev ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
return('{:04X}'.format(crc))
Thanks, it works. Is there a better way to convert the list to be in a
suitable format than this ChatGPT suggestion?
bits_string = ''.join(str(bit) for bit in input_list)
int_value = int(bits_string, 2)
hex_value = hex(int_value)
hex_string = hex_value.replace('0x', '0x0') if len(hex_value) % 2 == 1
else hex_value
hex_value = bytes.fromhex(hex_string[2:])
crc=crc16(hex_value)
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/oysstu/68072c44c02879a2abf94ef350d1c7c6#gistcomment-4550238>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AV562M34PM55UV4Y3T4S5PTXDIY53BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVAZTOMRWGU4DSOFHORZGSZ3HMVZKMY3SMVQXIZI>
.
You are receiving this email because you commented on the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
@pragma31 if I have a payload like this : " 623e0200000000000000000000000000" where 623e is crc given by a software i want to verify it , so what should i pass to get this value to the function that you have written here crc16 , thanks
same if i want to use https://crccalc.com/
@oysstu could you please guide me how could I use this code.
My data is (hex) E3BCBEBEA65EB3B87266124B2B238B6B
Polynomial is 0x1021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment