Related Tags

python

What is binascii.crc_hqx in Python?

The 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:

$x^{16}$ + $x^{12}$ + $x^{5}$ + ${1}$

Syntax

binascii.crc_hqx (data, crc)
// data is the string to be encoded.
// crc is the initial crc value. Often taken as zero.


Code

Let’s work through a few coding examples using crc_hqx.

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)


RELATED TAGS

python

CONTRIBUTOR