Example
Output the result of crc32():
<?php $str = crc32("Hello World!"); printf("%un",$str); ?>
Definition and usage
crc32() function calculates the 32 bits of a string CRC (Cyclic Redundancy Check).
This function can be used to verify data integrity.
Tip: To ensure that you get the correct string representation from the crc32() function, you must use the %u format character of the printf() or sprintf() function. If the %u format character is not used, the results may appear as incorrect numbers or negative numbers.
Syntax
crc32(string)
Parameter Description
string Required. Specifies the string to be calculated.
Technical details
Return value: Returns the 32-bit cyclic redundancy check code polynomial of string in the form of integer.
PHP version: 4.0.1+
Example 1
In this example, we will output with and without the "%u" format character The result of crc32() (note that the result is the same):
<?php $str = crc32("Hello world!"); echo 'Without %u: '.$str."<br>"; echo 'With %u: '; printf("%u",$str); ?>
The above code will output:
Without %u: 461707669 With %u: 461707669
Example 2
In this example, we will use And without using the "%u" format character, output the result of crc32() (note that the results are different):
<?php $str = crc32("Hello world."); echo 'Without %u: '.$str."<br>"; echo 'With %u: '; printf("%u",$str); ?>
The above code will output:
Without %u: -1959132156 With %u: 2335835140
crc32 returns The result will overflow on a 32-bit machine, so the result may be negative. On a 64-bit machine, there is no overflow, so it is always positive.
The CRC algorithm is calculated based on the number of bits in the word length.
The crc32 function will calculate PHP_INT_SIZE and PHP_INT_MAX according to the two constant references in php
The definition of these two constants:
The word length of the integer number is related to the platform, Although the usual maximum is about two billion (32-bit signed). PHP does not support unsigned integers. IntegerThe word length of the value can be represented by the constant PHP_INT_SIZE. Since PHP 4.4.0 and PHP 5.0.5, the maximum value can be represented by the constant PHP_INT_MAX.
Output PHP_INT_SIZE: 4 in the next 32 bits, PHP_INT_MAX: 2147483647
PHP_INT_SIZE: 8 in the next 64 bits in the output, PHP_INT_MAX: 9223372036854775807
The above is the detailed content of PHP function crc32() that calculates the 32-bit CRC of a string. For more information, please follow other related articles on the PHP Chinese website!