Java InetAddress from int2InetAddress(int val)

Here you can find the source of int2InetAddress(int val)

Description

Converts 32 bits int 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 int2InetAddress(int val) 

Method Source Code

//package com.java2s;
/**/*ww  w  .  j  a  va 2s.c  om*/
 * 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 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. inetAddress(String host)
  2. IntegerToInetAddress(int ipAddress)
  3. intToInetAddress(int i)
  4. ip2Long(InetAddress ip)
  5. ipToBytesByInetAddress(String ip)