Java IP Address to Long ipToLong(String ipAddress)

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

Description

ip To Long

License

Open Source License

Declaration

public static Long ipToLong(String ipAddress) 

Method Source Code

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

public class Main {
    public static Long ipToLong(String ipAddress) {
        Long result = 0L;/*  w  w  w .j  a v a2s  .  c  o  m*/
        String[] atoms = ipAddress.split("\\.");

        if (atoms.length != 4) {
            return null;
        }
        for (int i = 3; i >= 0; i--) {
            result |= (Long.parseLong(atoms[3 - i]) << (i * 8));
        }

        return result & 0xFFFFFFFF;
    }

    public static Long parseLong(String val, Long defaultVal) {
        Long ret = defaultVal;
        if (val != null && val.length() > 0) {
            try {
                ret = Long.parseLong(val);
            } catch (NumberFormatException e) {
            }
        }
        return ret;
    }

    public static long parseLong(String val, long defaultVal) {
        long ret = defaultVal;
        if (val != null && val.length() > 0) {
            try {
                ret = Long.parseLong(val);
            } catch (NumberFormatException e) {
            }
        }
        return ret;
    }
}

Related

  1. ipToLong(String ip)
  2. ipToLong(String ip)
  3. ipToLong(String ip)
  4. ipToLong(String ip)
  5. ipToLong(String ipAddress)
  6. ipToLong(String ipStr)
  7. ipToLong(String sip)
  8. ipToLong(String strIP)
  9. ipv4(long ip)