Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is binascii.crc_hqx in Python?

Sarvech Qadir

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:

x16x^{16} + x12x^{12} + x5x^{5} + 1{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

Sarvech Qadir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring