Here you can find the source of longToIpAddress(long ip)
Parameter | Description |
---|---|
ip | the long ip |
Parameter | Description |
---|---|
IllegalArgumentException | if <code>ip</code> is invalid |
ip
public static String longToIpAddress(long ip)
//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(); } }