Hello I'm having a database to select the IP location from>
The script was in php and I'm converting it to java but I have no idea what is the equivalent of ip2long('127.0.0.1' ));
in java
Hello I'm having a database to select the IP location from>
The script was in php and I'm converting it to java but I have no idea what is the equivalent of ip2long('127.0.0.1' ));
in java
Basically, this will convert your dotted IP address string to long.
public static Long Dot2LongIP(String dottedIP) {
String[] addrArray = dottedIP.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;
}
public static Long ipToLong(String stringIp) {
return Arrays.stream(stringIp.split("\\."))
.mapToLong(Long::parseLong)
.reduce(0,
(a, b) -> (a << 8) + b);
}
public static String ipToString(Long longIp){
return ((longIp >> 24) & 0xFF) + "." +
((longIp >> 16) & 0xFF) + "." +
((longIp >> 8) & 0xFF) + "." +
(longIp & 0xFF);
}
I don't think there is a standard API to do that in Java, but
1/ The InetAddress class gives you a method to get an array of byte.
2/ If you really need a single integer, you can use this snippet, found on http://www.myteneo.net/blog/-/blogs/java-ip-address-to-integer-and-back/
public static String intToIp(int i) {
return ((i >> 24 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
( i & 0xFF);
}
public static Long ipToInt(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;
}
I would start by converting the string to octets:
static final String DEC_IPV4_PATTERN = "^(([0-1]?\\d{1,2}\\.)|(2[0-4]\\d\\.)|(25[0-5]\\.)){3}(([0-1]?\\d{1,2})|(2[0-4]\\d)|(25[0-5]))$";
static byte[] toOctets(String address){
if(address==null){
throw new NullPointerException("The IPv4 address cannot be null.");
}
if(!address.matches(DEC_IPV4_PATTERN)){
throw new IllegalArgumentException(String.format("The IPv4 address is invalid:%s ",address));
}
//separate octets into individual strings
String[] numbers = address.split("\\.");
//convert octets to bytes.
byte[] octets = new byte[4];
for(int i = 0; i < octets.length; i++){
octets[i] = Integer.valueOf(numbers[i]).byteValue();
}
return octets;
}
And then the octets to a BigInteger since it accepts a byte array, and from it to an integer:
static int toInteger(byte[] octets){
if(octets==null){
throw new NullPointerException("The array of octets cannot be null");
}
if(octets.length != 4){
throw new IllegalArgumentException(String.format("The byte array must contain 4 octets: %d",octets.length));
}
return new BigInteger(octets).intValue();
}
And from here, you can simply do:
String address = "127.0.0.1";
System.out.println(toInteger(toOctets(address)));
Or create a function named ip2long(String address )
This one works for IPv4 and IPv6:
public static String toLongString(final String ip) {
try {
final InetAddress address = InetAddress.getByName(ip);
final byte[] bytes = address.getAddress();
final byte[] uBytes = new byte[bytes.length + 1]; // add one byte at the top to make it unsigned
System.arraycopy(bytes, 0, uBytes, 1, bytes.length);
return new BigInteger(uBytes).toString();
} catch (final UnknownHostException e) {
throw new IllegalArgumentException(e);
}
}
I hope below link will help you, clearly provided vice versa too
https://www.mkyong.com/java/java-convert-ip-address-to-decimal-number.
Provided methods from above link:
public static 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;
}
public static String longToIp(long ip) {
StringBuilder result = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
result.insert(0,Long.toString(ip & 0xff));
if (i < 3) {
result.insert(0,'.');
}
ip = ip >> 8;
}
return result.toString();
}