百科狗-知识改变命运!
--

crc32() - 计算一个字符串的 crc32 多项式 - php 字符串函数

百变鹏仔1年前 (2023-11-21)阅读数 20#技术干货
文章标签字符串

crc32()

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

计算一个字符串的 crc32 多项式

说明

crc32(string $str) :int

生成$str的 32 位循环冗余校验码多项式。这通常用于检查传输的数据是否完整。

Warning

crc32() - 计算一个字符串的 crc32 多项式 - php 字符串函数

由于 PHP 的整数是带符号的,所以在 32 位系统上许多 crc32 校验码将返回负整数。尽管在 64 位上所有crc32()的结果将都是正整数。

因此你需要使用sprintf()或printf()的“%u”格式符来获取表示无符号 crc32 校验码的字符串。

For a hexadecimal representation of the checksum you can either use the "%x" formatter ofsprintf()orprintf()or thedechex()conversion functions, both of these also take care of converting thecrc32()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 generichash().hash("crc32b",$str)will return the same string asdechex(crc32($str)).

参数

$str

要校验的数据。

返回值

返回$strcrc32 校验的整数。

范例

显示一个 crc32 校验码

示例中的第二行演示了如何使用printf()函数转换校验码:

参见

  • hash()生成哈希值(消息摘要)
  • md5()计算字符串的 MD5 散列值
  • sha1()计算字符串的 sha1 散列值
This function returns an unsigned integer from a 64-bit Linux platform. It does return the signed integer from other 32-bit platforms even a 64-bit Windows one.
The reason is because the two constants PHP_INT_SIZE and PHP_INT_MAX have different values on the 64-bit Linux platform.
I've created a work-around function to handle this situation.

Hope this helps.
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.
Here is my modified version :

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)