<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Calculates the crc32 polynomial of a string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.count-chars.html">count_chars</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.crypt.html">crypt</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.crc32" class="refentry"> <div class="refnamediv"> <h1 class="refname">crc32</h1> <p class="verinfo">(PHP 4 >= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">crc32</span> — <span class="dc-title">Calculates the crc32 polynomial of a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.crc32-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>crc32</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span> )</div> <p class="para rdfs-comment"> Generates the cyclic redundancy checksum polynomial of 32-bit lengths of the <em><code class="parameter">str</code></em>. This is usually used to validate the integrity of data being transmitted. </p> <div class="warning"><strong class="warning">Warning</strong> <p class="simpara"> Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms. On 64bit installations all <span class="function"><strong>crc32()</strong></span> results will be positive integers though. </p> <p class="simpara"> So you need to use the "%u" formatter of <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span> or <span class="function"><a href="function.printf.html" class="function">printf()</a></span> to get the string representation of the unsigned <span class="function"><strong>crc32()</strong></span> checksum in decimal format. </p> <p class="simpara"> For a hexadecimal representation of the checksum you can either use the "%x" formatter of <span class="function"><a href="function.sprintf.html" class="function">sprintf()</a></span> or <span class="function"><a href="function.printf.html" class="function">printf()</a></span> or the <span class="function"><a href="function.dechex.html" class="function">dechex()</a></span> conversion functions, both of these also take care of converting the <span class="function"><strong>crc32()</strong></span> result to an unsigned integer. </p> <p class="simpara"> 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. </p> <p class="simpara"> 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. <span class="function"><a href="function.md5.html" class="function">md5()</a></span> does) might have been a better plan to begin with. </p> <p class="simpara"> For a more portable solution you may also consider the generic <span class="function"><a href="function.hash.html" class="function">hash()</a></span>. <code class="code">hash("crc32b", $str)</code> will return the same string as <code class="code">dechex(crc32($str))</code>. </p> </div> </div> <div class="refsect1 parameters" id="refsect1-function.crc32-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">str</code></em></span> <dd> <p class="para"> The data. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.crc32-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the crc32 checksum of <em><code class="parameter">str</code></em> as an integer. </p> </div> <div class="refsect1 examples" id="refsect1-function.crc32-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4823"> <p><strong>Example #1 Displaying a crc32 checksum</strong></p> <div class="example-contents"><p> This example shows how to print a converted checksum with the <span class="function"><a href="function.printf.html" class="function">printf()</a></span> function: </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$checksum </span><span style="color: #007700">= </span><span style="color: #0000BB">crc32</span><span style="color: #007700">(</span><span style="color: #DD0000">"The quick brown fox jumped over the lazy dog."</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%u\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$checksum</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.crc32-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.hash.html" class="function" rel="rdfs-seeAlso">hash()</a> - Generate a hash value (message digest)</span></li> <li class="member"> <span class="function"><a href="function.md5.html" class="function" rel="rdfs-seeAlso">md5()</a> - Calculate the md5 hash of a string</span></li> <li class="member"> <span class="function"><a href="function.sha1.html" class="function" rel="rdfs-seeAlso">sha1()</a> - Calculate the sha1 hash of a string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.count-chars.html">count_chars</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.crypt.html">crypt</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>