Java IP Address to Long ip2Long(String ip)

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

Description

ip Long

License

Open Source License

Declaration

public static final long ip2Long(String ip) 

Method Source Code

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

public class Main {
    public static final long ip2Long(String ip) {
        if (ip == null)
            return 0;
        if (ip.startsWith("/"))
            ip = ip.substring(1);//from   w  w w. j a v a2s.  c  om
        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. convertIpString2Long(String IP)
  2. ip2Long(final String ip)
  3. ip2long(String ip)
  4. ip2long(String ip)
  5. ip2long(String ip)
  6. ip2long(String ip)
  7. ip2long(String ip)
  8. ip2long(String ip)
  9. ip2Long(String ip)