As mentioned in an article on the Internet: PHP's uniqid () function is the current subtlety multiplied by a larger number (1048576) and then into hexadecimal, with two points I do not understand:
1. Why multiply a fixed number in the conversion to 16, go straight to not?
2. Why switch to 16, decimal not?
Please enlighten me, thank you!
Reply content:
As mentioned in an article on the Internet: PHP's uniqid () function is the current subtlety multiplied by a larger number (1048576) and then into hexadecimal, with two points I do not understand:
1. Why multiply a fixed number in the conversion to 16, go straight to not?
2. Why switch to 16, decimal not?
Please enlighten me, thank you!
The first is not multiply, is to take the 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 still very clear, first take the second part of the system time and the microsecond part, because the microsecond part of the maximum value is 0xF423F
(999999), so the interception of the last five bits for the result of Uniqid, % 0x100000
is to intercept the last five bits (hex), 0x100000
Written in decimal is 1048576.
As for why hexadecimal, the hexadecimal digit is few, the computation speed is fast. The result of uniqid is not for people to read, there is no significant advantage in using decimal here.
Save the Microsecond information.
The 16 binary may be for bit operation or for space saving, but the space can be fully 36.
In fact, it should be converted to binary, because the binary is closest to the bottom of the computer, 0 for power outages, 1 for power, converted to 16 binary is just for the convenience of reading Ah, because the binary is a long time