php的crc32函数使用时需要注意的问题(不然就是坑
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了php的crc32函数使用时需要注意的问题(不然就是坑) ,需要的朋友可以参考下
前几天写了一个分表程序,用的hash算法是crc32.分表的函数如下:
代码如下:
function _getHash($username)
{
$hash = crc32($username) % 512;
return $hash;
}
function _getTable($username)
{
$hash = self::_getHash($username);
return 'user_' . $hash;
}
首先在本地32位window机上生成好数据并插入对应的表中。但是再把程序和数据传到服务器上(64为linux),发现查不到数据。经过排查后发现,原来服务器上crc32的结果和本地不同。再查php手册才知,crc32的接口原来和机器有关。
php手册的描述:
代码如下:
Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms. On 64bit installations all crc32() results will be positive integers though.
crc32返回的结果在32位机上会产生溢出,所以结果可能为负数。而在64位机上不会溢出,所以总是正值。
CRC算法是按字长位数bit进行计算的。
crc32函数会按照php中的两个常量参考计算 PHP_INT_SIZE,PHP_INT_MAX
这两个常量的定义:
整型数的字长和平台有关,尽管通常最大值是大约二十亿(32 位有符号)。PHP 不支持无符号整数。Integer值的字长可以用常量PHP_INT_SIZE来表示,自 PHP 4.4.0 和 PHP 5.0.5后,最大值可以用常量PHP_INT_MAX来表示。
输出下32位中PHP_INT_SIZE:4,PHP_INT_MAX:2147483647
输出下64位中PHP_INT_SIZE:8,PHP_INT_MAX:9223372036854775807
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程