PHP computes a string of 32-bit CRC function CRC32 ()

Source: Internet
Author: User
Tags crc32

Instance

Results of output CRC32 ():

<?php$str = CRC32 ("Hello world!"); printf ("%un", $str);? >

Definition and usage

The CRC32 () function computes a 32-bit CRC (cyclic redundancy check) for a string.

This function can be used to verify the integrity of the data.

Tip: To ensure that you can get the correct string representation from the CRC32 () function, you must use the%u format of the printf () or sprintf () function. If you do not use the%u format, the results may appear as incorrect numbers or negative values.

Grammar

CRC32 (String)

Parameter description

string is required. Specifies the string to be computed.

Technical details

Return value: A 32-bit cyclic redundancy check code polynomial that returns a string as an integer.

PHP version: 4.0.1+

Example 1

In this example, we will output the results of the CRC32 () (note the same result) in the case of using and not using the "%u" format character:

<?php$str = CRC32 ("Hello world!"); Echo ' without%u: '. $str. " <br> "; Echo ' with%u: ';p rintf ("%u ", $str);? >

The above code will output:

Without%u:461707669with%u:461707669

Example 2

In this example, we will output the results of CRC32 () without using the "%u" format character (note the result is not the same):

<?php$str = CRC32 ("Hello World"); Echo ' without%u: '. $str. " <br> "; Echo ' with%u: ';p rintf ("%u ", $str);? >

The above code will output:

Without%u: -1959132156with%u:2335835140

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

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.