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)).

A faster way I've found to return CRC values of larger files, is instead of using the file()/implode() method used below, is to us file_get_contents() (PHP 4 >= 4.3.0) which uses memory mapping techniques if supported by your OS to enhance performance. Here's my example function:

<?php
// $file is the path to the file you want to check.
function file_crc($file)
{
$file_string = file_get_contents($file);

$crc = crc32($file_string);

return sprintf("%u", $crc);
}

$file_to_crc = /home/path/to/file.jpg;

echo file_crc($file_to_crc); // Outputs CRC value for given file.
?>

I've found in testing this method is MUCH faster for larger binary files.

The khash() function by sukitsupaluk has two problems, it does not use all 62 characters from the $map set and when corrected it then produces different results on 64-bit compared to 32-bit PHP systems.

The crc32() function can return a signed integer in certain environments. Assuming that it will always return an unsigned integer is not portable.

Depending on your desired behavior, you should probably use sprintf() on the result or the generic hash() instead. Also note that integer arithmetic operators do not have the precision to work correctly with the integer output.

The crc32_combine() function provided by petteri at qred dot fi has a bug that causes an infinite loop, a shift operation on a 32-bit signed int might never reach zero. Replacing the function gf2_matrix_times() with the following seems to fix it:

function __crc32_string($text) { // Creates a CRC from a text string // Once the lookup table has been filled in by the two functions above, // this function creates all CRCs using only the lookup table.

// You need unsigned variables because negative values // introduce high bits where zero bits are required. // PHP doesn't have unsigned integers: // I've solved this problem by doing a '&' after a '>>'.

// Start out with all bits set high.$crc=0xffffffff;$len=strlen($text);

if you are looking for a fast function to hash a file, take a look athttp://www.php.net/manual/en/function.hash-file.phpthis is crc32 file checker based on a CRC32 guideit have performance at ~ 625 KB/s on my 2.2GHz Turionfar slower than hash_file('crc32b','filename.ext')<?phpfunction crc32_file ($filename){$f = @fopen($filename,'rb'); if (!$f) return false;

Note that the CRC32 algorithm should NOT be used for cryptographic purposes, or in situations where a hostile/untrusted user is involved, as it is far too easy to generate a hash collision for CRC32 (two different binary strings that have the same CRC32 hash). Instead consider SHA-1 or MD5.