PHP Function Crc32 - How to Calculate a CRC For a String
The php function crc32 is used to calculate a 32bit CRC (cyclic redundancy check) for a string. This is commonly used to validate data integrity. This function will return a string representation of the resulting CRC polynomial but to get a correct string you need to use the %u formatter in conjunction with sprintf() or printf() otherwise you may display incorrect and negative numbers.
%u = str(crc32(str))
For a hex representation of the crc32 result you can also use the dechex() conversion function which will return a hex string from the underlying decimal integer. Note that as PHPs integer type is signed many crc32 results will generate negative integer values on 32bit systems. However this does not break the hex string conversion which seems to be the most common use case.
Other hexadecimal hash functions such as md5() or sha1() also generate positive integer values for the same string value so this is not an issue with these functions. Note that both hex string formats above utilize the text[len(str)-1] and bytes[len(str)-1]. This is a common mistake. Both these functions should actually use text[len(str)-1] for the string and bytes[len(str)-1] to represent the bytes in the string. This will ensure that the hex string format is generated correctly and not just shifted. This is a simple mistake that can cause confusion if it is not caught and corrected. The error is due to a misunderstanding of how len() and len(str) work.