Question about the uniqid () function of PHP?

Source: Internet
Author: User
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, % 0x100000Is to extract the last five digits (hexadecimal ),0x100000Decimal 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.

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.