x
Solved

# Get all ips with a given range

Posted on 2003-12-11
Medium Priority
347 Views
I am a newbie so please be patient.  I have an IP range in which I would like to find all possible IPs within that range.  Also I'd like to be able to compare ip values in order to see which has a higher number.  I have not been able to find a class that enables me to do such.  Anyone know of anything?
0
Question by:tsr50
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 86

Accepted Solution

CEHJ earned 400 total points
ID: 9923124
You're welcome to use this to convert between numbers and ip addresses, from which you can construct a range:

public class InetRange {

public static void main(String[] args) {
long ip = InetRange.ipToLong(args[0]);
System.out.println(ip);
System.out.println(longToIp(ip));
}

public static String longToIp(long ipAddress) {
StringBuffer sb = new StringBuffer(15);
sb.append(octet1).append('.');
sb.append(octet2).append('.');
sb.append(octet3).append('.');
sb.append(octet4);
return sb.toString();
}

public static long ipToLong(String ipAddress) {
long result = 0;
try {
long octet1 = bytes[0] & 0xFF;
octet1 <<= 24;
long octet2 = bytes[1] & 0xFF;
octet2 <<= 16;
long octet3 = bytes[2] & 0xFF;
octet3 <<= 8;
long octet4 = bytes[3] & 0xFF;
result = octet1 | octet2 | octet3 | octet4;
}
catch(Exception e) {
e.printStackTrace();
return 0;
}
return result;
}

public static String intToIp(int ipAddress) {
int octet1 = (ipAddress & 0xFF000000) >> 24;
int octet2 = (ipAddress & 0xFF0000) >> 16;
int octet3 = (ipAddress & 0xFF00) >> 8;
int octet4 = ipAddress & 0xFF;
return new StringBuffer().append(octet1)
.append('.')
.append(octet2)
.append('.')
.append(octet3)
.append('.')
.append(octet4)
.toString();
}

}

0

LVL 86

Expert Comment

ID: 9930242
8-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month15 days, 8 hours left to enroll