Java IP Address to Int ipToInt(String ipAddr)

Here you can find the source of ipToInt(String ipAddr)

Description

ip To Int

License

Open Source License

Declaration

public static int ipToInt(String ipAddr) 

Method Source Code


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

import java.net.InetAddress;

public class Main {

    public static int ipToInt(String ipAddr) {
        try {/*from  w  w w  .  j av  a2s.  c  o m*/
            return bytesToInt(ipToBytesByInet(ipAddr));
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }

    public static int bytesToInt(byte[] bytes) {
        int addr = bytes[3] & 0xFF;
        addr |= ((bytes[2] << 8) & 0xFF00);
        addr |= ((bytes[1] << 16) & 0xFF0000);
        addr |= ((bytes[0] << 24) & 0xFF000000);
        return addr;
    }

    public static byte[] ipToBytesByInet(String ipAddr) {
        try {
            return InetAddress.getByName(ipAddr).getAddress();
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }
}

Related

  1. ip2int(String ipAddr)
  2. ipToInt(final String addr)
  3. ipToInt(String addr)
  4. ipToInt(String address)
  5. ipToInt(String ip)
  6. IpToInt(String strIp)
  7. ipToInteger(String ip)
  8. IPV4AddressStrToInteger(String ipAddressStr)
  9. IPV4AddressToInteger(byte[] addr)