Java InetAddress from long2InetAddress(long val)

Here you can find the source of long2InetAddress(long val)

Description

Converts 32 bits int packaged into a 64bits long to IPv4 InetAddress.

License

Open Source License

Parameter

Parameter Description
val int representation of IPv4 address

Return

the address object

Declaration

public static final InetAddress long2InetAddress(long val) 

Method Source Code

//package com.java2s;
/**/*from ww  w  .j a va2  s .  c  o m*/
 * Copyright (c) 2014-2016 openHAB UG (haftungsbeschraenkt) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.net.InetAddress;

import java.net.UnknownHostException;

public class Main {
    /**
     * Converts 32 bits int packaged into a 64bits long to IPv4 <tt>InetAddress</tt>.
     *
     * @param val int representation of IPv4 address
     * @return the address object
     */
    public static final InetAddress long2InetAddress(long val) {
        if ((val < 0) || (val > 0xFFFFFFFFL)) {
            // TODO exception ???
        }
        return int2InetAddress((int) val);
    }

    /**
     * Converts 32 bits int to IPv4 <tt>InetAddress</tt>.
     *
     * @param val int representation of IPv4 address
     * @return the address object
     */
    public static final InetAddress int2InetAddress(int val) {
        byte[] value = { (byte) ((val & 0xFF000000) >>> 24), (byte) ((val & 0X00FF0000) >>> 16),
                (byte) ((val & 0x0000FF00) >>> 8), (byte) ((val & 0x000000FF)) };
        try {
            return InetAddress.getByAddress(value);
        } catch (UnknownHostException e) {
            return null;
        }
    }
}

Related

  1. IntegerToInetAddress(int ipAddress)
  2. intToInetAddress(int i)
  3. ip2Long(InetAddress ip)
  4. ipToBytesByInetAddress(String ip)
  5. ipToLong(InetAddress ip)
  6. toInetAddress(String host)