crc32()

创建于 2024-12-03 / 23
字体: [默认] [大] [更大]

❮ PHP String 字符串参考手册

实例

输出 crc32() 的结果:

<?php
$str = crc32("Hello World!");
printf("%u\n",$str);
?> 亲自试一试 »

定义和用法

crc32() 函数计算字符串的 32 位 CRC(循环冗余校验)。

该函数可用于验证数据完整性。

提示:为了确保从 crc32() 函数中获得正确的字符串表示,您需要使用 printf() 或 sprintf() 函数的 %u 格式符。如果未使用 %u 格式符,结果可能会显示为不正确的数字或者负数。


语法

crc32(string)

参数值

参数 描述
string 必需。规定要计算的字符串。


技术细节

返回值: 以整数值返回字符串的 32 位循环冗余校验码多项式。
PHP 版本: 4.0.1+

更多实例

实例

在本实例中,我们将在使用以及不使用 "%u" 格式符的情况下,输出 crc32() 的结果(注意结果是相同的):

<?php
$str = crc32("Hello world!");
echo 'Without %u: '.$str."<br>";
echo 'With %u: ';
printf("%u",$str);
?>

上述代码的输出为:

Without %u: 461707669
With %u: 461707669

实例

在本实例中,我们将在使用以及不使用 "%u" 格式符的情况下,输出 crc32() 的结果(注意结果是不相同的):

<?php
$str = crc32("Hello world.");
echo 'Without %u: '.$str."<br>";
echo 'With %u: ';
printf("%u",$str);
?>

上述代码的输出为:

Without %u: -1959132156
With %u: 2335835140
❮ PHP String 字符串参考手册
0 人点赞过