Java IP Address to Long ip2long(String ip)

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

Description

iplong

License

Open Source License

Declaration

public static long ip2long(String ip) throws NumberFormatException, IllegalArgumentException 

Method Source Code

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

public class Main {
    public static long ip2long(String ip) throws NumberFormatException, IllegalArgumentException {
        String[] parts = ip.split("\\.");
        long result = 0;
        int power = 0;
        if (parts.length != 4) {
            throw new IllegalArgumentException("Invalid IP format");
        }/*w  w w.  j a v  a  2s. c  o  m*/
        for (int i = parts.length; i > 0; i--) {
            int parseInt = Integer.parseInt(parts[i - 1]);
            if (parseInt < 0 || parseInt > 255) {
                throw new IllegalArgumentException("Invalid IP address");
            }
            result += Math.pow(256, power++) * parseInt;
        }
        return result;
    }
}

Related

  1. convertIPS2Long(String ip)
  2. convertIpString2Long(String IP)
  3. ip2Long(final String ip)
  4. ip2long(String ip)
  5. ip2long(String ip)
  6. ip2Long(String ip)
  7. ip2long(String ip)
  8. ip2long(String ip)