Java IP Address to Int ip2Int(String ip)

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

Description

ip Int

License

Open Source License

Declaration

public static final int ip2Int(String ip) 

Method Source Code

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

public class Main {

    public static final int ip2Int(String ip) {
        return (int) ip2Long(ip);
    }//from   www  . j a  v  a  2s . c o  m

    public static final long ip2Long(String ip) {
        if (ip == null)
            return 0;
        if (ip.startsWith("/"))
            ip = ip.substring(1);
        int i = ip.indexOf(":");
        if (i > 0)
            ip = ip.substring(0, i);

        String[] strs = ip.split("\\.");
        long longIp = 0;
        if (strs.length == 4) {
            for (i = 3; i >= 0; i--) {
                longIp += (Long.parseLong(strs[3 - i]) << (i * 8));
            }
        } else if (strs.length == 6) {
            for (i = 5; i >= 0; i--) {
                longIp += (Long.parseLong(strs[5 - i]) << (i * 8));
            }
        }
        return longIp;
    }
}

Related

  1. ip2Int(String addr)
  2. ip2int(String ip)
  3. IP2Int(String ip)
  4. ip2int(String ipAddr)
  5. ipToInt(final String addr)
  6. ipToInt(String addr)