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...
hash
Cheng10
In 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.