Notes for using php crc32 functions (otherwise it is a pitfall)

Source: Internet
Author: User
Tags crc32
This article mainly introduces the issues that need to be paid attention to when using php's crc32 function (otherwise, it is a pitfall). If you need it, you can refer to a table sharding program written in the next few days, the hash algorithm is crc32. the table sharding function is as follows:

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 data on the local 32-bit window machine and insert the corresponding table. However, when the program and data are uploaded to the server (64 for linux), no data is found. After troubleshooting, we found that the crc32 results on the original server are different from those on the local server. Check the php manual again to know that the crc32 interface was originally related to the machine.
Php manual description:

The code is as follows:


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


The results returned by crc32 may overflow on 32-bit machines, so the results may be negative. On a 64-bit host, it does not overflow, so it is always positive.

The CRC algorithm is calculated based on the number of characters in the long bits.

The crc32 function calculates PHP_INT_SIZE and PHP_INT_MAX according to the two constants in php.
Definitions of these two constants:
The length of the integer is related to the platform, although the maximum value is usually about 2 billion (32-bit signed ). PHP does not support unsigned integers. The length of the Integer value can be expressed by the constant PHP_INT_SIZE. after PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed by the constant PHP_INT_MAX.
Output the following 32-bit PHP_INT_SIZE: 4, PHP_INT_MAX: 2147483647
Output 64-bit PHP_INT_SIZE: 8, PHP_INT_MAX: 9223372036854775807

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.