How to calculate the CRC32 of a string in PHP

1 Answer

0 votes
/**
 * Calculates the CRC32 checksum of a string.
 *
 * @param string $input The string to hash.
 * @return string The CRC32 value as an 8‑character uppercase hex string.
 */
function crc32_of_string(string $input): string
{
    // crc32() returns a signed integer on some systems,
    // so we convert it to an unsigned 32‑bit value.
    $crc = crc32($input);

    // Format as 8‑digit uppercase hexadecimal (standard CRC32 representation)
    return strtoupper(sprintf("%08X", $crc));
}

$text = "PHP Programming";

// Compute the CRC32 of the string
$crc = crc32_of_string($text);

echo "CRC32: $crc\n";



/*
run:

CRC32: 96F84E6F

*/

 



answered May 8 by avibootz
...