Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

crc-16-ccitt


import binascii

def generate_hqx():
    polynomial = 0x1021
    table = []
    for byte in range(256):
        crc = byte << 8
        for _ in range(8):
            if crc & 0x8000:
                crc = ((crc << 1) ^ polynomial) & 0xFFFF
            else:
                crc = (crc << 1) & 0xFFFF
        table.append(crc)
    return table

def calc_crc_hqx(bts):
    table = generate_hqx()
    crc = 0x0000
    for b in bts:
        index = (crc >> 8) ^ b
        crc = ((crc << 8) & 0xFFFF) ^ table[index]
    return crc

s = 'd74ae47dc6f599d3f9cb847bd77d6b7c'
assert(calc_crc_hqx(binascii.unhexlify(s)) == binascii.crc_hqx(binascii.unhexlify(s), 0))

You'll only receive email when they publish something new.

More from 38617
All posts