How does a md5-PHP hash a string into a 10-digit number?

Source: Internet
Author: User
Tags crc32
Md5 is a string that is hash into a character + number, but now I need to hash a string into a 10-digit number. Is there any algorithm that can be implemented, or, you can convert the md5 result to a 10-digit md5. This method is hash into a character + number string, but now I need to hash a string into a 10-digit number, is there any algorithm that can be implemented, or the md5 result can be converted into a 10-digit

Reply content:

Md5 is a string that is hash into a character + number, but now I need to hash a string into a 10-digit number. Is there any algorithm that can be implemented, or, you can convert the md5 result to a decimal one.

In general, the crc32 () function can be used to convert strings to decimal numbers...

The advantage of this function is that it is fast... in my knowledge, this should be the fastest built-in hash function in php...

The disadvantage is that the operating system depends on different operating systems...crc32()The function will generate different values...

Asmd5()It is obviously unreliable to use hexdec (). The reason is as described above...

Here we use another more flexible hexadecimal conversion function base_convert ()...

The benefit of this function is that it returns string... so there is no overflow problem...

The sample code is as follows...

                                         

hashCheng10In hexadecimal notationbig number

You can use the php hexdec function to segment the md5 result, because the maximum hexdec value can only be changed to 0x7fffffff, and then to float.

In addition, you do not have to use md5 because the md5 result is bits, and the php language itself cannot directly store such a large decimal number. You can refer to sdbmhash, jshash, or C ++ STL to implement a string hash. For example, this is python sdbmhash.

def sdbm_hash_string(str):    h = 0    m = (1 << 32)    for i in str:        t = h        h = (t << 6) % m + (t << 16) % m - t + ord(i)        h %= m    return h

Finally, by the way, do not try to split the md5 result into several segments and then XOR or the like. It is better to take the last few bytes directly.

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.