Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

java - How do I get generate an IP address range given start and end IP address?

How can generate a range of IP addresses from a start and end IP address?

Example for a network "192.168.0.0/24":

String start = "192.168.0.2"
String end = "192.168.0.254"

I want 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 above is just an example.

Thanks...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Recognize that each of the 4 components of an IPv4 address is really a hex number between 00 and FF.

If you change your start and end IP addresses into 32 bit unsigned integers, you can just loop from the lowest one to the highest one and convert each value you loop through back into the IP address format.

The range in the example you give is C0A80002 to C0A800FE.

Here's a link to code that converts between a hex number and an IPv4 address

http://technojeeves.com/joomla/index.php/free/58-convert-ip-address-to-number


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...