Java IP Address to Int ipv4ToInt(String addr)

Here you can find the source of ipv4ToInt(String addr)

Description

ipv To Int

License

Open Source License

Declaration

public static int ipv4ToInt(String addr) throws UnknownHostException 

Method Source Code

//package com.java2s;
/*/*from  ww  w  . j  a v  a 2 s . co  m*/
 * Copyright (c) 2015 Yale University and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

import java.net.Inet4Address;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    public static int ipv4ToInt(String addr) throws UnknownHostException {
        Inet4Address a = (Inet4Address) InetAddress.getByName(addr);
        byte[] b = a.getAddress();
        int addrInt = ((b[0] & 0xFF) << 24) | ((b[1] & 0xFF) << 16)
                | ((b[2] & 0xFF) << 8) | ((b[3] & 0xFF) << 0);
        return addrInt;
    }
}

Related

  1. IpToInt(String strIp)
  2. ipToInteger(String ip)
  3. IPV4AddressStrToInteger(String ipAddressStr)
  4. IPV4AddressToInteger(byte[] addr)
  5. ipV4ToInt(String addr)
  6. ipv4ToInt(String address)