The implementation principle of ip2long and long2ip functions in PHP. Recently I want to make a reversible conversion of decimal numbers for the invitation code. I haven't figured out how to make it. it's too complicated. I think this can be converted when I get an IP address today, so I studied how to implement reversible conversion of decimal numbers for the invitation code. I have never figured out how to do it. it is too complicated. I think this can be converted when I get an IP address today, so I studied the principle:
I have sorted it out by myself:
$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, 24))) .'.';$str .= intval(($result&0x00FFFFFF)/intval(pow(2, 16))) .'.';$str .= intval(($result&0x0000FFFF)/intval(pow(2, 8))) .'.';$str .= intval($result&0x000000FF);echo $str;echo '
';echo long2ip($result);
The output result is as follows:
20356923020356923012.34.56.7812.34.56.78
This is just one way to achieve it, and there are other ways
I want to use a similar method to convert a decimal number to another decimal value and convert it to reversible. However, when there are many numbers with 0 in the middle, I find that there are too many problems, such as 1000100.
...