Java IP Address to String ipV4ToLong(final String ipaddress)

Here you can find the source of ipV4ToLong(final String ipaddress)

Description

Convert an IP Address to a long.

License

Open Source License

Parameter

Parameter Description
ipaddress a parameter

Exception

Parameter Description
UnknownHostException an exception
SecurityException an exception

Return

long

Declaration


public static long ipV4ToLong(final String ipaddress) throws UnknownHostException, SecurityException 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

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

public class Main {
    /***********************************************************************************************
     * Convert an IP Address to a long./*  w  w w . j a  v  a2 s .c o  m*/
     *
     * @param ipaddress
     *
     * @return long
     *
     * @throws UnknownHostException
     * @throws SecurityException
     */

    public static long ipV4ToLong(final String ipaddress) throws UnknownHostException, SecurityException {
        final String SOURCE = "NetworkScannerHelper.ipV4ToLong() ";
        final byte[] arrayBytes;
        final int octet1;
        final int octet2;
        final int octet3;
        final int octet4;
        long longAddress;

        //LOGGER.logTimedEvent(SOURCE + "String [address=" + ipaddress + "]");

        arrayBytes = InetAddress.getByName(ipaddress).getAddress();
        //LOGGER.logTimedEvent(SOURCE + "Bytes [address=" + Utilities.byteArrayToSpacedHex(arrayBytes) + "]");

        octet1 = (arrayBytes[0] & 0xFF) << 24;
        octet2 = (arrayBytes[1] & 0xFF) << 16;
        octet3 = (arrayBytes[2] & 0xFF) << 8;
        octet4 = arrayBytes[3] & 0xFF;

        longAddress = octet1 | octet2 | octet3 | octet4;
        longAddress = longAddress & 0x00000000FFFFFFFFL;

        //LOGGER.logTimedEvent(SOURCE + "Long [address=" + longAddress + "]");

        return (longAddress);
    }
}

Related

  1. ipToString(final byte[] bytes)
  2. IpToString(int address)
  3. IPToString(int intIP)
  4. ipToString(long ip)
  5. ipv4ToBinaryStr(int ipv4)
  6. ipv4ToStr(int ipv4)
  7. ipv4ToString(byte[] address)
  8. ipV4ToString(int ip)