Java IP Address to Long ipToLong(String ip)

Here you can find the source of ipToLong(String ip)

Description

Converts a String IP address to a long.

License

Open Source License

Parameter

Parameter Description
ip the IP address to convert

Return

a long representation of the IP address

Declaration

public static long ipToLong(String ip) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w  ww .  j  av a2s  .c  om*/
     * Converts a String IP address to a long.
     *
     * @param ip the IP address to convert
     * @return a long representation of the IP address
     */
    public static long ipToLong(String ip) {

        String[] ipArray = ip.split("\\.");

        long num = 0;

        for (int i = 0; i < ipArray.length; i++) {

            int power = 3 - i;

            num += ((Integer.parseInt(ipArray[i]) % 256 * Math.pow(256, power)));

        }

        return num;

    }
}

Related

  1. ipToLong(byte[] octets)
  2. ipToLong(final String addr)
  3. ipToLong(String addr)
  4. ipTolong(String address)
  5. ipToLong(String ip)
  6. ipToLong(String ip)
  7. ipToLong(String ip)
  8. ipToLong(String ip)
  9. ipToLong(String ipAddress)