Python .crc_hqx()

Anonymous contributor's avatar
Anonymous contributor
Published Nov 5, 2025
Contribute to Docs

The .crc_hqx() function in Python’s built-in binascii module computes a 16-bit Cyclic Redundancy Check (CRC) value using the CRC-HQX algorithm, commonly used in the original Mac OS.

This checksum helps detect errors in data transmission or storage by verifying that the received data matches the original data.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

binascii.crc_hqx(data, value)

Parameters:

  • data (bytes-like object): The binary data for which the CRC value is computed.
  • value (integer): An optional 16-bit starting value for the checksum calculation. If omitted, the initial value is 0.

Return value:

Returns a 16-bit integer representing the computed CRC checksum of the input data, according to the CRC-HQX algorithm.

Example

In this example, the checksum is computed for a byte string, once with a non-zero starting value and once with an initial value of 0:

import binascii
data = b"Codecademy Docs"
initial_crc = 0xAAAA # A common non-zero starting value
# 1. Compute CRC with a starting value (0xAAAA)
crc_with_initial = binascii.crc_hqx(data, initial_crc)
# 2. Compute CRC starting from 0
crc_without_initial = binascii.crc_hqx(data, 0)
print(f"Data: {data!r}")
print(f"CRC (with initial value 0xAAAA): 0x{crc_with_initial:04x}")
print(f"CRC (starting from 0): 0x{crc_without_initial:04x}")

The output of this code is:

Data: b'Codecademy Docs'
CRC (with initial value 0xAAAA): 0x134c
CRC (starting from 0): 0x67ce

Codebyte Example

This example below computes the CRC-HQX checksum for a byte string using a starting value of 0:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours