Java InetAddress from ip2Long(InetAddress ip)

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

Description

Converts an IP v4 number to a 64bit (long) number, according to the lobby protocol standard.

License

Open Source License

Parameter

Parameter Description
ip an IP v4 (<tt>Inet4Address</tt>)

Return

a 64 bit number representing the supplied IP

Declaration

public static long ip2Long(InetAddress ip) 

Method Source Code

//package com.java2s;
/*/*ww  w .j  a v  a2s .c  o m*/
   Copyright (c) 2011 Robin Vobruba <hoijui.quaero@gmail.com>
    
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.net.InetAddress;

public class Main {
    /**
     * Converts an IP v4 number to a 64bit (long) number, according to the lobby
     * protocol standard.
     * @param ip an IP v4 (<tt>Inet4Address</tt>)
     * @return a 64 bit number representing the supplied IP
     */
    public static long ip2Long(InetAddress ip) {

        long res;

        byte[] addr = ip.getAddress();
        final long f1 = (long) addr[0] << 24;
        final long f2 = (long) addr[1] << 16;
        final long f3 = (long) addr[2] << 8;
        final long f4 = (long) addr[3];
        res = f1 + f2 + f3 + f4;

        return res;
    }
}

Related

  1. inetAddress(String host)
  2. int2InetAddress(int val)
  3. IntegerToInetAddress(int ipAddress)
  4. intToInetAddress(int i)
  5. ipToBytesByInetAddress(String ip)
  6. ipToLong(InetAddress ip)
  7. long2InetAddress(long val)
  8. toInetAddress(String host)