Java Utililty Methods IP Address to Long

List of utility methods to do IP Address to Long

Description

The list of methods to do IP Address to Long are organized into topic(s).

Method

longip2Long(String ip)
ip Long
long ret = 0;
if (ip == null) {
    return ret;
String[] segs = ip.split("\\.");
for (int i = 0; i < segs.length; i++) {
    long seg = Long.parseLong(segs[i]);
    ret += (seg << ((3 - i) * 8));
...
longip2Long(String ipAddress)
ip Long
String[] ipAddressInArray = ipAddress.split("[.]");
long result = 0;
for (int i = 3; i >= 0; i--) {
    long ip = Long.parseLong(ipAddressInArray[3 - i]);
    result |= ip << (i * 8);
return result;
longip2Long(String ipaddress)
ip Long
long[] ip = new long[4];
int position1 = ipaddress.indexOf(".");
int position2 = ipaddress.indexOf(".", position1 + 1);
int position3 = ipaddress.indexOf(".", position2 + 1);
ip[0] = Long.parseLong(ipaddress.substring(0, position1));
ip[1] = Long.parseLong(ipaddress.substring(position1 + 1, position2));
ip[2] = Long.parseLong(ipaddress.substring(position2 + 1, position3));
ip[3] = Long.parseLong(ipaddress.substring(position3 + 1));
...
longipToLong(byte[] address)
A convenient method that accepts an IP address represented by a byte[] of size 4 and returns this as a long representation of the same IP address.
if (address.length != 4) {
    throw new IllegalArgumentException("byte array must be of length 4");
long ipNum = 0;
long multiplier = 1;
for (int i = 3; i >= 0; i--) {
    int byteVal = (address[i] + 256) % 256;
    ipNum += byteVal * multiplier;
...
longipToLong(byte[] octets)
ip To Long
long result = 0;
for (byte octet : octets) {
    result <<= 8;
    result |= octet & 0xff;
return result;
longipToLong(final String addr)
Converts the text form of an IP address to a long.
final String[] addressBytes = addr.split("\\.");
if (addressBytes.length != 4) {
    throw new IllegalArgumentException("IPs have the format x.x.x.x");
long ip = 0;
for (int i = 0; i < 4; i++) {
    ip <<= 8;
    ip |= Integer.parseInt(addressBytes[i]);
...
longipToLong(String addr)
ip To Long
String ipAddress = addr;
long netMask = 0;
if (addr.contains(SLASH)) {
    String[] addressAndSubnet = addr.split(SLASH);
    netMask = addrsInSlashnet(Integer.valueOf(addressAndSubnet[1]));
    ipAddress = addressAndSubnet[0];
String[] addrArray = ipAddress.split("\\.");
...
longipTolong(String address)
Get IP Address in Long from String.
long[] ip = new long[4];
String[] parts = address.split("\\.");
for (int i = 0; i < 4; i++) {
    ip[i] = Long.parseLong(parts[i]);
long ipNumbers = 0;
for (int i = 0; i < 4; i++) {
    ipNumbers += ip[i] << (24 - (8 * i));
...
longipToLong(String ip)
Get a long representation of the specified IP Address.
long result = 0;
String[] ipAddressInArray = ip.split("\\.");
for (int i = 3; i >= 0; i--) {
    long lip = Long.parseLong(ipAddressInArray[3 - i]);
    result |= lip << (i * 8);
return result;
longipToLong(String ip)
Converts a String IP address to a long.
String[] ipArray = ip.split("\\.");
long num = 0;
for (int i = 0; i < ipArray.length; i++) {
    int power = 3 - i;
    num += ((Integer.parseInt(ipArray[i]) % 256 * Math.pow(256, power)));
return num;