Java InetAddress convertInetAddressToLong(InetAddress addr)

Here you can find the source of convertInetAddressToLong(InetAddress addr)

Description

convert Inet Address To Long

License

LGPL

Declaration

public static long convertInetAddressToLong(InetAddress addr) 

Method Source Code

//package com.java2s;
/*//  w w  w .ja v  a 2  s .c o  m
 * IRClib -- A Java Internet Relay Chat library -- class IRCConnection
 * Copyright (C) 2002 - 2006 Christoph Schwering <schwering@gmail.com>
 * 
 * This library and the accompanying materials are made available under the
 * terms of the
 *    - GNU Lesser General Public License,
 *    - Apache License, Version 2.0 and
 *    - Eclipse Public License v1.0.
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY.
 */

import java.net.InetAddress;

public class Main {
    public static long convertInetAddressToLong(InetAddress addr) {
        // FIXME The documentation doesn't say that hashCode() has to return
        // the address in network byte order, so other Java implementations
        // probably don't do so, and I think this results in a sent negative
        // number sent to the recipient which might not be understood by all
        // clients, because the CTCP specification says this has to be an 
        // unsigned integer.
        return addr.hashCode();

        // I don't know why, but the following doesn't work due to (at least to
        // me: wired) conversions between int and long (Java adds leading zeros,
        // some times
        //      byte[] arr = addr.getAddress();
        //      long address = 0;
        //      address |= ((long)(arr[0] & 0xFF)) << 24;
        //      System.out.println("1 address = "+ Long.toBinaryString(address) +" (arr[0] = "+ Integer.toBinaryString(arr[0]&0xFF) +")");
        //      address |= ((long)(arr[1] & 0xFF)) << 12;
        //      System.out.println("2 address = "+ Long.toBinaryString(address) +" (arr[1] = "+ Integer.toBinaryString(arr[1]&0xFF) +")");
        //      address |= ((long)(arr[2] & 0xFF)) << 8;
        //      System.out.println("3 address = "+ Long.toBinaryString(address) +" (arr[2] = "+ Integer.toBinaryString(arr[2]&0xFF) +")");
        //      address |= ((long)(arr[3] & 0xFF));
        //      System.out.println("4 address = "+ Long.toBinaryString(address) +" (arr[3] = "+ Integer.toBinaryString(arr[3]&0xFF) +")");
        //      System.out.println(Long.toBinaryString(addr.hashCode()));
        //      System.out.println(Integer.toBinaryString(addr.hashCode()));
        //      System.out.println(Integer.toBinaryString(arr[0]&0xFF) +" "+ Integer.toBinaryString(arr[1]&0xFF) +" "+ Integer.toBinaryString(arr[2]&0xFF) +" "+ Integer.toBinaryString(arr[3]&0xFF));
        //      System.out.println("address = "+ address);
        //      return address & 0xFFFFFFFF;
    }
}

Related

  1. compareAddresses(InetAddress one, InetAddress two)
  2. compareInetAddresses(InetAddress a, InetAddress b)
  3. compareInetAddresses(InetAddress a1, InetAddress a2)
  4. CompareIP(InetAddress ip1, InetAddress ip2)
  5. contains(InetAddress network, InetAddress netmask, InetAddress ip)
  6. convertInetAddressToLong(InetAddress address)
  7. convertLongToInetAddress(long address)
  8. createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot)
  9. createDatagramSocket(InetAddress addr, int port)