Here you can find the source of ipV4AddressToLong(String ipAddress)
Parameter | Description |
---|---|
ipAddress | a parameter |
Parameter | Description |
---|---|
UnknownHostException | if the address is not a valid IPv4 address |
public static long ipV4AddressToLong(String ipAddress) throws UnknownHostException
//package com.java2s; //License from project: Open Source License import java.net.InetAddress; import java.net.UnknownHostException; public class Main { /**/* www.j av a2 s. co m*/ * Converts an IP address string like 192.168.0.1 to an internal long value * * @param ipAddress * @return long the numerical representation of the address * @throws UnknownHostException if the address is not a valid IPv4 address */ public static long ipV4AddressToLong(String ipAddress) throws UnknownHostException { byte[] bytes = InetAddress.getByName(ipAddress).getAddress(); return convertByteToLong(bytes); } private static long convertByteToLong(byte[] ipBytes) { long unsignedInt = 0; for (int i = 0; i < ipBytes.length; i++) { unsignedInt |= ((0x000000FF & ((int) ipBytes[i])) << ((ipBytes.length - i - 1) * 8)); } return unsignedInt & 0xFFFFFFFFL; } }