PHP CRC32 functions need to be aware of problems _php tutorial

Source: Internet
Author: User
Tags crc32

Questions to be aware of when using PHP's CRC32 function


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

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

Copy the code code as follows:

function _gethash ($username)

{

$hash = CRC32 ($username)% 512;

return $hash;

}

function _gettable ($username)

{

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

Return ' User_ '. $hash;

}

First generate the good data on the local 32-bit window machine and insert the corresponding table. But then the program and data 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 again to know, CRC32 interface was originally related to the machine.

Description of the PHP manual:

Copy the code code as follows:

Because PHP ' s integer type is signed many CRC32 checksums would 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 a 32-bit machine, so the result may be negative. The 64-bit machine does not overflow, so it is always positive.

The CRC algorithm is calculated by bit-length bits.

The CRC32 function is calculated as a reference to the two constants in PHP Php_int_size,php_int_max

The definitions of these two constants:

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

Output under 32 bits in php_int_size:4,php_int_max:2147483647

Output under 64 bits in php_int_size:8,php_int_max:9223372036854775807

http://www.bkjia.com/PHPjc/987718.html www.bkjia.com true http://www.bkjia.com/PHPjc/987718.html techarticle PHP CRC32 functions to be aware of the problem this article mainly introduces the use of PHP CRC32 function to pay attention to the problem (or pit), the need for friends can refer to the next few days to write ...

  • 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.