Java Long to IP Address longToIpAddress(long ip)

Here you can find the source of longToIpAddress(long ip)

Description

Returns the 32bit dotted format of the provided long ip.

License

Mozilla Public License

Parameter

Parameter Description
ip the long ip

Exception

Parameter Description
IllegalArgumentException if <code>ip</code> is invalid

Return

the 32bit dotted format of ip

Declaration

public static String longToIpAddress(long ip) 

Method Source Code

//package com.java2s;
/**/*from   w  w w  .  ja v a  2 s  . c om*/
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

public class Main {
    /**
     * Returns the 32bit dotted format of the provided long ip.
     *
     * @param ip the long ip
     * @return the 32bit dotted format of <code>ip</code>
     * @throws IllegalArgumentException if <code>ip</code> is invalid
     */
    public static String longToIpAddress(long ip) {
        // if ip is bigger than 255.255.255.255 or smaller than 0.0.0.0
        if (ip > 4294967295l || ip < 0) {
            throw new IllegalArgumentException("invalid ip");
        }

        StringBuilder ipAddress = new StringBuilder();

        for (int i = 3; i >= 0; i--) {
            int shift = i * 8;
            ipAddress.append((ip & (0xff << shift)) >> shift);
            if (i > 0) {
                ipAddress.append(".");
            }
        }

        return ipAddress.toString();
    }
}

Related

  1. longToIp(long ip)
  2. longToIp(long ip)
  3. LongToIp(long longIp)
  4. longToIp(long longValue)
  5. longToIp(String ip)
  6. longToIPv4(final long ipaddress)
  7. longToIpV4(long ip)
  8. longToIpV4(long longIp)
  9. longToIpV4(long longIp)