crc32

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

crc32锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷 crc32 锟斤拷锟斤拷式

说锟斤拷

crc32 ( string $str ) : int

锟斤拷锟斤拷 str 锟斤拷 32 位循锟斤拷锟斤拷锟斤拷校锟斤拷锟斤拷锟斤拷锟绞斤拷锟斤拷锟酵拷锟斤拷锟斤拷诩锟介传锟斤拷锟斤拷锟斤拷锟斤拷欠锟斤拷锟斤拷锟斤拷锟�

Warning

锟斤拷锟斤拷 PHP 锟斤拷锟斤拷锟斤拷锟角达拷锟斤拷锟脚的o拷锟斤拷锟斤拷锟斤拷 32 位系统锟斤拷锟斤拷锟� crc32 校锟斤拷锟诫将锟斤拷锟截革拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷 64 位锟斤拷锟斤拷锟斤拷 crc32() 锟侥斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�

锟斤拷锟斤拷锟斤拷锟揭癸拷锟� sprintf() 锟斤拷 printf() 锟斤拷"%u"锟斤拷式锟斤拷锟斤拷锟斤拷取锟斤拷示锟睫凤拷锟斤拷 crc32 校锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟�

For a hexadecimal representation of the checksum you can either use the "%x" formatter of sprintf() or printf() or the dechex() conversion functions, both of these also take care of converting the crc32() result to an unsigned integer.

Having 64bit installations also return negative integers for higher result values was considered but would break the hexadecimal conversion as negatives would get an extra 0xFFFFFFFF######## offset then. As hexadecimal representation seems to be the most common use case we decided to not break this even if it breaks direct decimal comparisons in about 50% of the cases when moving from 32 to 64bits.

In retrospect having the function return an integer maybe wasn't the best idea and returning a hex string representation right away (as e.g. md5() does) might have been a better plan to begin with.

For a more portable solution you may also consider the generic hash(). hash("crc32b", $str) will return the same string as dechex(crc32($str)).

锟斤拷锟斤拷

str

要校锟斤拷锟斤拷锟斤拷荨锟�

锟斤拷锟斤拷值

锟斤拷锟斤拷 str crc32 校锟斤拷锟斤拷锟斤拷锟斤拷锟�

锟斤拷锟斤拷

Example #1 锟斤拷示一锟斤拷 crc32 校锟斤拷锟斤拷

示锟斤拷锟叫的第讹拷锟斤拷锟斤拷示锟斤拷锟斤拷锟绞癸拷锟� printf() 锟斤拷锟斤拷转锟斤拷校锟斤拷锟诫:

<?php
$checksum 
crc32("The quick brown fox jumped over the lazy dog.");
printf("%u\n"$checksum);
?>

锟轿硷拷

  • hash() - 锟斤拷锟缴癸拷希值 锟斤拷锟斤拷息摘要锟斤拷
  • md5() - 锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷 MD5 散锟斤拷值
  • sha1() - 锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷 sha1 散锟斤拷值