The implementation principle of PHP's Ip2long and LONG2IP functions

Source: Internet
Author: User
Tags functions get ip pow

Recently to do a decimal number of reversible conversion to do the invitation code, has not figured out how to do it, it is too complicated, today to get IP thought this can be converted, and then studied the following principle:

The main is to organize themselves under:


$ip = ' 12.34.56.78 ';
$ips = Explode ('. ', $ip);
$result = 0;
$result + + $ips [0]<<24;
$result + + $ips [1]<<16;
$result + + $ips [2]<<8;
$result + + $ips [3];
Echo Bindec (Decbin ($result));
Echo '
'; Echo Bindec (Decbin (Ip2long ($IP))); Echo '
'; $str = '; $str. = Intval ($result/intval (POW (2)). $str. = Intval (($result &0x00ffffff)/intval (POW (2)). $str. = Intval (($result &0x0000ffff)/intval (POW (2, 8)). $str. = Intval ($result &0x000000ff); echo $str; Echo '
'; echo Long2ip ($result);

The results of the above output are:

203569230
203569230
12.34.56.78
12.34.56.78


It's just a way to achieve it, and there are other ways.

I would like to use a similar method, you can directly convert a decimal number to other decimal values and reversible, but encountered a number of 0 in the middle of the number of problems found too much, such as 1000100






Related Article

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.