PHP implements hash value calculation (high-precision PHP computing)

Source: Internet
Author: User

Recently, PHP is used to implement a hashAlgorithm, The problem and solution are as follows:

1. php only supports signed integers.Signed and unsigned numbers.

A positive integer (64-bit) stores the corresponding value, and a negative number stores the corresponding value as a supplementary code.

 
Function strtoint ($ Str) {If (bccomp ($ STR, '000000')> = 0) {$ STR = bcsub ('20140901', $ Str ); $ STR = "-". $ STR;} else if (bccomp ($ STR, '-100') <0) {$ STR = bcadd ('20140901', $ Str);} return (INT) $ STR ;}

2. php does not support the Left shift of unsigned integers. If it is a negative number, you need to manually convert it to the corresponding unsigned number.

3. php cannot perform type conversion (I mean (Static_cast<Int>)Char *). If you want to convert a bytecode to an int, You need to convert each character to an ASCII value.

Function addfor64a ($ data) {$ sum = bcadd (string) (ord ($ data [0]), (string) (ord ($ data [1]) <8); $ sum = bcadd ($ sum, (string) (ord ($ data [2]) <16); $ sum = bcadd ($ sum, (string) (ord ($ data [3]) <24); $ sum = bcadd ($ sum, (string) (ord ($ data [4]) <32); $ sum = bcadd ($ sum, (string) (ord ($ data [5]) <40); $ sum = bcadd ($ sum, (string) (ord ($ data [6]) <48); $ sum = bcadd ($ sum, (string) (ord ($ data [7]) <56); return $ this-> strtoint ($ sum );}

After the problem is clarified, it is not difficult to calculate the hash.

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.