mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
131 lines
6.0 KiB
HTML
131 lines
6.0 KiB
HTML
<!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>计算一个字符串的 crc32 多项式</title>
|
|
</head>
|
|
<body class="docs"><div id="layout">
|
|
<div id="layout-content"><div id="function.crc32" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">crc32</h1>
|
|
<p class="verinfo">(PHP 4 >= 4.0.1, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">crc32</span> — <span class="dc-title">计算一个字符串的 crc32 多项式</span></p>
|
|
|
|
</div>
|
|
|
|
<div class="refsect1 description" id="refsect1-function.crc32-description">
|
|
<h3 class="title">说明</h3>
|
|
<div class="methodsynopsis dc-description">
|
|
<span class="methodname"><strong>crc32</strong></span>
|
|
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
|
|
) : <span class="type">int</span></div>
|
|
|
|
<p class="para rdfs-comment">
|
|
生成 <code class="parameter">str</code>
|
|
的 32 位循环冗余校验码多项式。这通常用于检查传输的数据是否完整。
|
|
</p>
|
|
<div class="warning"><strong class="warning">Warning</strong>
|
|
<p class="simpara">
|
|
由于 PHP 的整数是带符号的,所以在 32 位系统上许多 crc32 校验码将返回负整数。
|
|
尽管在 64 位上所有 <span class="function"><strong>crc32()</strong></span> 的结果将都是正整数。
|
|
</p>
|
|
<p class="simpara">
|
|
因此你需要使用
|
|
<span class="function"><a href="sprintf.html" class="function">sprintf()</a></span> 或 <span class="function"><a href="printf.html" class="function">printf()</a></span>
|
|
的“%u”格式符来获取表示无符号 crc32 校验码的字符串。
|
|
</p>
|
|
<p class="simpara">
|
|
For a hexadecimal representation of the checksum you can either use the
|
|
"%x" formatter of <span class="function"><a href="sprintf.html" class="function">sprintf()</a></span> or <span class="function"><a href="printf.html" class="function">printf()</a></span>
|
|
or the <span class="function"><a href="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="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="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">参数</h3>
|
|
<p class="para">
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">str</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
要校验的数据。
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.crc32-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
返回 <code class="parameter">str</code> crc32 校验的整数。
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.crc32-examples">
|
|
<h3 class="title">范例</h3>
|
|
<p class="para">
|
|
<div class="example" id="example-5898">
|
|
<p><strong>Example #1 显示一个 crc32 校验码</strong></p>
|
|
<div class="example-contents"><p>
|
|
示例中的第二行演示了如何使用 <span class="function"><a href="printf.html" class="function">printf()</a></span>
|
|
函数转换校验码:
|
|
</p></div>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><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>
|
|
</pre></div>
|
|
</div>
|
|
|
|
</div>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 seealso" id="refsect1-function.crc32-seealso">
|
|
<h3 class="title">参见</h3>
|
|
<p class="para">
|
|
<ul class="simplelist">
|
|
<li class="member"><span class="function"><a href="hash.html" class="function" rel="rdfs-seeAlso">hash()</a> - 生成哈希值 (消息摘要)</span></li>
|
|
<li class="member"><span class="function"><a href="md5.html" class="function" rel="rdfs-seeAlso">md5()</a> - 计算字符串的 MD5 散列值</span></li>
|
|
<li class="member"><span class="function"><a href="sha1.html" class="function" rel="rdfs-seeAlso">sha1()</a> - 计算字符串的 sha1 散列值</span></li>
|
|
</ul>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
|
|
</div></div></div></body></html> |