For more information about the causes and solutions for negative php ip2long values, see ip2long.
Php provides ip2long and long2ip methods for IP address processing.
1. ip2long-convert an IPV4 string Internet protocol to a digital format
Int ip2long (string $ ip_address)
Parameters:Ip_address: a standard format address.
Return Value:Returns the number or FALSE after the IP address is converted. If ip_address is invalid.
2. long2ip-convert the digital format into an IPV4 string Internet Protocol
String long2ip (string $ proper_address)
Parameters:The correct address representation of the proper_address long integer.
Return Value:Returns the Internet address as a string.
3. Usage
<?php$ip = '10.1.1.1';$ip_long = ip2long($ip);echo $ip_long.PHP_EOL; // 167837953echo long2ip($ip_long); // 10.1.1.1?>
4. Causes of negative numbers and Solutions
When the IP address is large, ip2long will show a negative number:
<?php$ip = '192.168.101.100';$ip_long = ip2long($ip);echo $ip_long.PHP_EOL; // -1062705820echo long2ip($ip_long); // 192.168.101.100?>
Cause description:
IPv4 uses an unsigned 32-bit address, so there can be a maximum of 2 32 power minus 1 (4294967295) addresses. The decimal number separated by four decimal points.
For example, 192.168.100.100.
Each 10-digit IPv4 address is an unsigned byte in the range of 0 ~ 255. to convert an IPv4 address to an unsigned number, each 10-digit number is placed on the corresponding 8-bit to form a 4-byte unsigned integer. 192.168.100.100, 192,168 in high 8 bits 100,100 in low 8 bits.
Example of C implementation:
#include <stdio.h>int main(int argc, char** argv){ unsigned int ip_long = (192 << 24) | (168 << 16) | (100 << 8) | 100; printf("%u\n", ip_long); printf("%d\n", ip_long); return 0;}fdipzone@ubuntu:~/C$ gcc -o ip2long ip2long.cfdipzone@ubuntu:~/C$ ./ip2long3232261220-1062706076
As you can see, even if the ip_long Declaration is an unsigned integer, you still need to specify % u in the output to format the output as an unsigned integer.
Because 192 is greater than 127 (01111111 in binary format), 192 (8 bits) is represented in binary format, and the highest bit must be 1. The highest bit of the 4-byte integer is 1.
Although ip_long is defined as an unsigned integer, the printf method will not be declared. Therefore, % u format is required for output. If the maximum bit is 0, use % d.
Another example:
ip:112.24.55.99#include <stdio.h>int main(int argc, char** argv){ unsigned int ip_long = (112 << 24) | (24 << 16) | (55 << 8) | 99; printf("%u\n", ip_long); printf("%d\n", ip_long); return 0;}fdipzone@ubuntu:~/C$ gcc -o ip2long ip2long.cfdipzone@ubuntu:~/C$ ./ip2long18806352351880635235
Solution:
% U is used to format the output as an unsigned integer.
<?php$ip = '192.168.101.100';$ip_long = sprintf('%u',ip2long($ip));echo $ip_long.PHP_EOL; // 3232261476 echo long2ip($ip_long); // 192.168.101.100?>
The reason for the negative number of php ip2long and the solution are all the content that I have shared with you in this article. I hope to give you a reference, and I hope you can provide more support to the customer's house.