Two questions about PHP's uniqid () function?

Source: Internet
Author: User
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

  • 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.