Java InetAddress to toLong(InetAddress ip)

Here you can find the source of toLong(InetAddress ip)

Description

to Long

License

Creative Commons License

Declaration

public static long toLong(InetAddress ip) 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import java.net.InetAddress;

public class Main {
    public static long toLong(String ip) {
        String[] addressArray = ip.split("\\.");
        long result = 0;

        for (int i = 0; i < addressArray.length; i++) {
            int power = 3 - i;

            result += ((Integer.parseInt(addressArray[i]) % 256 * Math.pow(256, power)));
        }//from   w  w w  . ja  v  a2s.  c o m

        return result;
    }

    public static long toLong(InetAddress ip) {
        return toLong(getIP(ip));
    }

    public static String getIP(InetAddress ip) {
        return ip.getHostAddress().replace("/", "");
    }
}

Related

  1. toAddrString(InetAddress ip)
  2. toDotString(InetAddress ip)
  3. toIntArray(InetAddress addr)
  4. toIpAddrString(final InetAddress addr)
  5. toLong(InetAddress addr)