binascii is a widely used Python library for ASCII-encoded binary representations. It contains several methods for converting to binary from ASCII or hex, and vice versa.
binascii.crc_hqx computes the 16-bit CRC value of the provided data. It takes an initial CRC (in the binhex4 format) as one of the parameters.
Often the initial CRC is taken as 0.
The method makes use of the CRC-CCITT polynomial:
+ + +
binascii.crc_hqx (data, crc) // data is the string to be encoded. // crc is the initial crc value. Often taken as zero.
Let’s work through a few coding examples using
In these examples, the
b(string)means that the string is in bytes form.
import binascii # compute crc of a few string. Initial parameter is taken as 0. crc = binascii.crc_hqx(b"Hello World", 0) print(crc) crc = binascii.crc_hqx(b"Welcome", 0) print(crc) # let's use the crc calculated above as parameter crc = binascii.crc_hqx(b"Welcome to Educative", crc) print(crc)
View all Courses