As mentioned in an article on the Internet, the uniqid () function of PHP is to multiply the current subtlety by a large number (1048576) and then convert it to hexadecimal, I don't understand either of the following two points: 1. why is it impossible to convert a fixed number into a hexadecimal system by multiplying it? 2. why do we need to convert to hexadecimal format... as mentioned in an article on the Internet, the uniqid () function of PHP is to multiply the current subtlety by a large number (1048576) and then convert it to hexadecimal, I don't understand either of the following two points:
1. Why is it impossible to convert a fixed number into a hexadecimal system by multiplying it?
2. Why do we need to convert to hexadecimal instead of decimal?
Thank you!
Reply content:
As mentioned in an article on the Internet, the uniqid () function of PHP is to multiply the current subtlety by a large number (1048576) and then convert it to hexadecimal, I don't understand either of the following two points:
1. Why is it impossible to convert a fixed number into a hexadecimal system by multiplying it?
2. Why do we need to convert to hexadecimal instead of decimal?
Thank you!
First, it is not a multiplication, but a remainder.
Https://github.com/php/php-src/blob/php-7.0.0/ext/standard/uniqid.c#L72-L78
gettimeofday((struct timeval *) &tv, (struct timezone *) NULL); sec = (int) tv.tv_sec; usec = (int) (tv.tv_usec % 0x100000); /* The max value usec can have is 0xF423F, so we use only five hex * digits for usecs. */
This code is very clear. First, take the second part of the system time and the microsecond part, because the maximum value of the microsecond part is0xF423F
(999999), so the last five digits are intercepted for the uniqid result, % 0x100000
Is to extract the last five digits (hexadecimal ),0x100000
Decimal format: 1048576
As for the reason why the hexadecimal system has few digits and the computing speed is fast. The result of uniqid is not for people to read. Here there is no significant advantage in decimal format.
Save the microsecond information.
The hexadecimal format may be for bitwise operations or space saving, but the 36 hexadecimal format can be used for saving space.
In fact, the conversion should be binary, because the binary is the most close to the underlying computer, 0 is power-off, 1 is power-on, and the conversion to hexadecimal is only for easy reading, because the conversion to binary is a long segment.