Home > Backend Development > PHP Tutorial > PHP calculates 32-bit crc (cyclic redundancy check) of a string

PHP calculates 32-bit crc (cyclic redundancy check) of a string

藏色散人
Release: 2023-04-05 15:12:01
Original
3459 people have browsed it

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.

PHP calculates 32-bit crc (cyclic redundancy check) of a string

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)
Copy after login

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 &#39;没有%u: &#39;.$str1."\n"; 
echo &#39;使用%u: &#39;; 
  
printf("%u\n", $str1); 
?>
Copy after login

Output:

没有%u: -1959132156 
使用%u: 2335835140
Copy after login

Example 2: Calculate the 32-bit CRC of the string "PHPandJava.", both with and without %u.

<?php
$str2 = crc32("PHPandJava.");

echo &#39;没有%u: &#39;.$str2."\n";
echo &#39;使用%u: &#39;;

printf("%u\n", $str2);
?>
Copy after login

Output:

没有%u: -650239106 
使用%u: 3644728190
Copy after login

Example 3: Compute the 32-bit CRC of the string "Computer Science." with and without %u.

<?php 
$str3 = crc32("Computer Science."); 
  
echo &#39;没有%u: &#39;.$str3."\n"; 
echo &#39;使用%u: &#39;; 
  
printf("%u\n", $str3); 
?>
Copy after login

Output:

没有%u: -1082893780 
使用%u: 3212073516
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
PHP-Datenerfassung?
From 1970-01-01 08:00:00
0
0
0
pemerolehan data php?
From 1970-01-01 08:00:00
0
0
0
PHP 데이터 수집?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template