Java InetAddress to addressToLong(InetAddress address)

Here you can find the source of addressToLong(InetAddress address)

Description

Converts the bytes that make up an internet address into the corresponding integer value to make it easier to perform bit-masking operations on them.

License

Open Source License

Parameter

Parameter Description
address an address whose integer equivalent is desired

Return

the integer corresponding to that address

Declaration

@SuppressWarnings("WeakerAccess")
public static long addressToLong(InetAddress address) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.InetAddress;

public class Main {
    /**/*  w w w  .j  ava 2s.  co m*/
     * Converts the bytes that make up an internet address into the corresponding integer value to make
     * it easier to perform bit-masking operations on them.
     *
     * @param address an address whose integer equivalent is desired
     *
     * @return the integer corresponding to that address
     */
    @SuppressWarnings("WeakerAccess")
    public static long addressToLong(InetAddress address) {
        long result = 0;
        for (byte element : address.getAddress()) {
            result = (result << 8) + unsign(element);
        }
        return result;
    }

    /**
     * Converts a signed byte to its unsigned int equivalent in the range 0-255.
     *
     * @param b a byte value to be considered an unsigned integer
     *
     * @return the unsigned version of the byte
     */
    public static int unsign(byte b) {
        return b & 0xff;
    }
}

Related

  1. inetAddress2Int(InetAddress addr)
  2. InetAddressToInt(InetAddress ip)
  3. inetAddressToThriftBinary(InetAddress inetAddress)
  4. ipv4AddressToInt(InetAddress addr)