# Get all ips with a given range

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?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
8-)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.