如何根据开始和结束IP地址生成IP地址范围? [英] How do I get generate an IP address range given start and end IP address?

查看:728
本文介绍了如何根据开始和结束IP地址生成IP地址范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从起始和结束IP地址生成一系列IP地址?

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

网络示例192.168.0.0/24:

Example for a network "192.168.0.0/24":

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

我希望:

192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
...
192.168.0.254

PS:网络,开始和结束IP可以是动态上面只是一个例子。

PS: Network, start and end IP can be dynamic above is just an example.

谢谢...

推荐答案

认识到IPv4地址的4个组成部分中的每一个都是00到FF之间的十六进制数。

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

如果你改变你的开始和结束IP地址为32位无符号整数,您可以从最低的一个循环到最高的一个,并将您循环的每个值转换回IP地址格式。

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.

您给出的示例中的范围是C0A80002到C0A800FE。

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

这是一个代码的链接,可以在十六进制数和IPv4地址之间进行转换s

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

这篇关于如何根据开始和结束IP地址生成IP地址范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆