Lastly I worked with an application where I need to have range of IP's which implies few conditions 'from' range should be smaller than 'to' range. To use long data type make IP's easier to sort, comparing range and and for persistence.
So here are two java method which can convert IP into a long and vice versa.
Conversion of IP String to long data type:
public long ipToLong(String addr) {
String[] addrArray = addr.split("\\.");
long num = 0;
for (int i = 0; i < addrArray.length; i++) {
int power = 3 - i;
num += ((Integer.parseInt(addrArray[i]) % 256 * Math.pow(256, power)));
}
return num;
}
Conversion of long data type to IP String :
public String lognToIp(long l) {
return ((l >> 24) & 0xFF) + "." +
((l >> 16) & 0xFF) + "." +
((l >> 8) & 0xFF) + "." +
(l & 0xFF);
}
Ref: http://teneo.wordpress.com/2008/12/23/java-ip-address-to-integer-and-back/