Java – how to generate an IP address range given the start and end IP addresses?
•
Java
How do I generate a series of IP addresses from the start and end IP addresses?
Network example "192.168.0.0/24":
String start = "192.168.0.2" String end = "192.168.0.254"
I hope to have:
192.168.0.2 192.168.0.3 192.168.0.4 192.168.0.5 ... 192.168.0.254
PS: network, start and end IP can be dynamic. The above is just an example
Thank you
Solution
Recognize that each of the four components of the IPv4 address is a hexadecimal number between 00 and FF
If you change the start and end IP addresses to 32-bit unsigned integers, you can cycle from the lowest to the highest, and convert each value of the cycle back to the IP address format
The range in the example you give is c0a80002 to c0a800fe
This is a code link that converts between hexadecimal numbers and IPv4 addresses
http://technojeeves.com/joomla/index.php/free/58-convert-ip-address-to-number
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码