Java ip2long equivalent

convert ip address to integer java
convert ip address to long
how to convert ip address to integer

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;    
}

Java - Convert IP address to Decimal Number, java-ip-to-decimal. In this tutorial, we show you how to convert an IP address to its decimal equivalent in Java, and vice versa. For examples :. Here’s what our current JavaScript equivalent to PHP's ip2long looks like.


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;

}

PHP ip2long() Function, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  The function ip2long() generates an long integer representation of IPv4 Internet network address from its Internet standard format (dotted string) representation. ip2long() will also work with non-complete IP addresses.


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 )

ip2long, ip2long — Converts a string containing an (IPv4) Internet Protocol dotted address into a long integer $out = "The following URLs are equivalent:<br />\n"; $out . int ip2long ( string $ip_address ) Parameters: This function accepts single parameter as mentioned above and described below: $ip_address: It is required parameter which specifies an IP address. Return Value: This function returns a long integer on success or FALSE on failure.


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);
    }
}

PHP, The ip2long() function is an inbuilt function in PHP which converts IPv4 address (​dotted IP These three URLs are equivalent: 1. https://geeksforgeeks.org/ 2. PHP - Function ip2long - It used to convert ipv4 address to long integer.


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();
}

ip2long - Manual, ip2long — Converts a string containing an (IPv4) Internet Protocol dotted address FORMAT (that I can think of) and will return it's decimal unsigned equivalent,  Java allows you to play online games, chat with people around the world, calculate your mortgage interest, and view images in 3D, just to name a few. Java software for your computer, or the Java Runtime Environment, is also referred to as the Java Runtime, Runtime Environment, Runtime, JRE, Java Virtual Machine, Virtual Machine, Java VM, JVM


IP2Long.java, PHP - Function ip2long - It used to convert ipv4 address to long integer. $out = "The following URLs are equivalent: \n"; $out .= 'http://www.tutorialspoint.com/,  PHP Version: 4.0+ PHP Changelog: PHP 5.5: Before this version, on Windows, this function might return a valid number even if the passed value was not an IPv4 address. PHP 5.2: PHP 5.5: Before this version, this function might return a valid number even if the passed value was not an IPv4 address.


PHP - Function ip2long, to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. PHP 7 ip2long() Function $out = "The following URLs are equivalent:<br>"; Prior to this version, on Windows ip2long() would sometimes return a valid number even if passed a value which was not an (IPv4) Internet Protocol dotted address. 5.2.10: Prior to this version, ip2long() would sometimes return a valid number even if passed a value which was not an (IPv4) Internet Protocol dotted address.


PHP 7 ip2long() Function - W3Schools, Interface Comparable<T>. Type Parameters: T - the type of objects that this object may be compared to. Java equivalent is most likely Groovy. At the expense of loosing type safety though – gnat Jul 16 '13 at 18:07 You can use Java Functional Utils to convert C#'s 101 LINQ examples .. – mythz Aug 1 '15 at 0:30