crc32() function is used to calculate the 32-bit cyclic redundancy check code polynomial of a string. This function uses the CRC32 algorithm. This function can be used to verify data integrity.
However, to ensure that we get the correct string representation from the crc32() function, we need to use the %u formatter of the printf() or sprintf() function. If you do not use the %u formatter, the results may display incorrect negative numbers.
crc32() function syntax:
crc32($string)
Parameters:
$ string: This parameter specifies the string for which we want to find the crc32 polynomial.
Return value: The crc32() function returns the crc32 checksum of the given string as an integer.
Example 1: Calculate the 32-bit CRC of the string "Hello World", including with and without %u.
<?php $str1 = crc32("Hello world."); echo '没有%u: '.$str1."\n"; echo '使用%u: '; printf("%u\n", $str1); ?>
Output:
没有%u: -1959132156 使用%u: 2335835140
Example 2: Calculate the 32-bit CRC of the string "PHPandJava.", both with and without %u.
<?php $str2 = crc32("PHPandJava."); echo '没有%u: '.$str2."\n"; echo '使用%u: '; printf("%u\n", $str2); ?>
Output:
没有%u: -650239106 使用%u: 3644728190
Example 3: Compute the 32-bit CRC of the string "Computer Science." with and without %u.
<?php $str3 = crc32("Computer Science."); echo '没有%u: '.$str3."\n"; echo '使用%u: '; printf("%u\n", $str3); ?>
Output:
没有%u: -1082893780 使用%u: 3212073516
Related recommendations: "PHP Tutorial"
This article is an introduction to the 32-bit crc of PHP calculation strings , I hope it will be helpful to friends in need!
The above is the detailed content of PHP calculates 32-bit crc (cyclic redundancy check) of a string. For more information, please follow other related articles on the PHP Chinese website!