Home >Backend Development >PHP Tutorial >Issues that need to be paid attention to when using PHP's crc32 function (otherwise it will be a pitfall), crc32 function_PHP tutorial

Issues that need to be paid attention to when using PHP's crc32 function (otherwise it will be a pitfall), crc32 function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:56:22997browse

Things that need to be paid attention to when using the crc32 function of PHP (otherwise it will be a pitfall). The crc32 function

I wrote a table splitting program a few days ago. The hash algorithm used is crc32. The functions of the sub-table are as follows:
Copy code The code is as follows:
Function _getHash($username)
{
          $hash = crc32($username) % 512;
         return $hash;
}

function _getTable($username)
{
          $hash = self::_getHash($username);
          return 'user_' . $hash;
}

First, generate the data on the local 32-bit window machine and insert it into the corresponding table. But when I transferred the program and data to the server (64 for Linux), I found that the data could not be found. After investigation, it was discovered that the crc32 results on the server were different from those locally. After checking the PHP manual again, I found out that the crc32 interface is related to the machine.
Description of php manual:
Copy code The code is as follows:
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.

The result returned by crc32 will overflow on a 32-bit machine, so the result may be negative. On a 64-bit machine, there is no overflow, so it is always positive.

The CRC algorithm is calculated based on the number of bits in the word length.

The crc32 function will calculate PHP_INT_SIZE and PHP_INT_MAX according to the two constant references in php
Definition of these two constants:
The word size of integers is platform-dependent, although the usual maximum is about two billion (32-bit signed). PHP does not support unsigned integers. The word length of an Integer value can be represented by the constant PHP_INT_SIZE. Since PHP 4.4.0 and PHP 5.0.5, the maximum value can be represented by the constant PHP_INT_MAX.
Output the next 32 bits PHP_INT_SIZE: 4, PHP_INT_MAX: 2147483647
Output PHP_INT_SIZE: 8, PHP_INT_MAX: 9223372036854775807

in 64-bit output

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/987896.htmlTechArticleProblems that need to be paid attention to when using the crc32 function of PHP (otherwise it will be a pitfall), I wrote a crc32 function a few days ago Table splitting program, the hash algorithm used is crc32. The table splitting function is as follows: Copy the code code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn