Java IP Address to Long convertIpString2Long(String IP)

Here you can find the source of convertIpString2Long(String IP)

Description

Parse the long number of IPv4 address from dot-separated string

License

Open Source License

Parameter

Parameter Description
IP a parameter

Declaration

public static long convertIpString2Long(String IP) 

Method Source Code

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

public class Main {
    /**//ww w  .ja v a2s  .co  m
     * Parse the long number of IPv4 address from dot-separated string
     *
     * @param IP
     * @return
     */
    public static long convertIpString2Long(String IP) {
        String[] addrArray = IP.split("\\.");
        if (addrArray.length != 4) {
            throw new NumberFormatException("Invalid IP string format: "
                    + IP);
        }
        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;
    }
}

Related

  1. convertIPS2Long(String ip)
  2. ip2Long(final String ip)
  3. ip2long(String ip)
  4. ip2long(String ip)
  5. ip2long(String ip)