Here you can find the source of ipv4AddressToInt(InetAddress addr)
Parameter | Description |
---|---|
addr | IPv4 INET address. |
Parameter | Description |
---|---|
IllegalArgumentException | if the address is really an IPv6 address |
public static int ipv4AddressToInt(InetAddress addr)
//package com.java2s; //License from project: Open Source License import java.net.Inet6Address; import java.net.InetAddress; public class Main { /** /*from w ww . j a v a2 s . com*/ * Convert an IPv4 INET address to an integer. * @param addr IPv4 INET address. * @return integer representation of a given address. * @throws IllegalArgumentException if the address is really an IPv6 address */ public static int ipv4AddressToInt(InetAddress addr) { if (addr instanceof Inet6Address) throw new IllegalArgumentException("IPv6 address used in IPv4 context"); byte[] a = addr.getAddress(); int res = ((a[0] & 0xFF) << 24) | ((a[1] & 0xFF) << 16) | ((a[2] & 0xFF) << 8) | (a[3] & 0xFF); return res; } }