An IPv4 can have more representations: as string (a.b.c.d) or numerical (as an unsigned int of 32 bits). (Maybe other, but I will ignore them.)
Is there any
Heres is a way to Convert IP to Number. I found it a valid way to accomplish the task in Java.
public long ipToLong(String ipAddress) {
String[] ipAddressInArray = ipAddress.split("\\.");
long result = 0;
for (int i = 0; i < ipAddressInArray.length; i++) {
int power = 3 - i;
int ip = Integer.parseInt(ipAddressInArray[i]);
result += ip * Math.pow(256, power);
}
return result;
}
This is also how you would implement it in Scala.
def convertIPToLong(ipAddress: String): Long = {
val ipAddressInArray = ipAddress.split("\\.")
var result = 0L
for (i <- 0 to ipAddressInArray.length-1) {
val power = 3 - i
val ip = ipAddressInArray(i).toInt
val longIP = (ip * Math.pow(256, power)).toLong
result = result +longIP
}
result
}