Problems needing attention when using PHP's CRC32 function

Source: Internet
Author: User
Tags constant crc32 manual hash

This article mainly introduces the use of PHP CRC32 function need to pay attention to the problem (otherwise is the pit), the need for friends can refer to the

A few days ago wrote a table program, using the hash algorithm is CRC32. The function of the table is as follows:

Copy code code as follows:

function _gethash ($username)

{

$hash = CRC32 ($username)% 512;

return $hash;

}

function _gettable ($username)

{

$hash = Self::_gethash ($username);

Return ' User_ '. $hash;

}

First, the data is generated on the local 32-bit window machine and inserted into the corresponding table. But then the program and data uploaded to the server (64 for Linux), found no data. After the investigation found that the original server CRC32 results and local differences. Check the PHP manual to know that the interface between the original and the Machine CRC32.

Description of the PHP manual:

Copy code code as follows:

Because PHP ' s integer type is signed many CRC32 checksums'll result in negative integers on 32bit platforms. On 64bit installations all CRC32 () results'll be positive integers though.

The result returned by CRC32 will overflow on the 32-bit machine, so the result may be negative. The 64-bit machine will not overflow, so always positive.

The CRC algorithm is calculated by the bit of the word length digit.

The CRC32 function is calculated according to the two constants referenced in PHP Php_int_size,php_int_max

The definitions of these two constants are:

The length of the integer is related to the platform, although usually the maximum value is approximately 2 billion (32-bit signed). PHP does not support unsigned integers. The length of the integer value can be represented by a constant php_int_size, which can be represented by a constant php_int_max since the PHP 4.4.0 and PHP 5.0.5.

32-bit php_int_size:4,php_int_max:2147483647 in output

64-bit php_int_size:8,php_int_max:9223372036854775807 in output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.