I'm wanting to calculate the CRC32 checksum of a string of hex values in python. I found zlib.crc32(data) and binascii.crc32(data), but all the examples I found using these functions have 'data' as a string ('hello' for example). I want to pass hex values in as data and find the checksum. I've tried setting data as a hex value (0x18329a7e for example) and I get a TypeError: must be string or buffer, not int. The function evaluates when I make the hex value a string ('0x18329a7e' for example), but I don't think it's evaluating the correct checksum. Any help would be appreciated. Thanks!
4
votes
What exactly you mean by "pass hex values in"? Do you realize that hexadecimal is just a numeric representation for humans, and that CRC32 applies to binary data? It is not clear what exactly you want to do: to calculate the CRC32 of a string containing hex characters, or of some binary data that happens to be represented as hexadecimal values somewhere in your software.
– Juliano
I want to calculate the CRC32 of some binary data that happens to be represented as hexadecimal values somewhere in my software.
– dmranck
Then Andrew's answer does it.
– Juliano
3 Answers
12
votes
I think you are looking for binascii.a2b_hex()
:
>>> binascii.crc32(binascii.a2b_hex('18329a7e'))
-1357533383
1
votes