Java Long to IP Address longToIp(long i)

Here you can find the source of longToIp(long i)

Description

long To Ip

License

Open Source License

Declaration

public static String longToIp(long i) 

Method Source Code

//package com.java2s;

public class Main {
    private static final int SLASHNET_VALUE = 32;
    private static final String DOT = ".";
    private static final int X_FF = 0xFF;
    private static final int EIGHT = 8;
    private static final int SIXTEEN = 16;
    private static final int TWENTY_FOUR = 24;
    private static final String SLASH = "/";
    private static final int NUMBER_OF_BITS_IN_IP = 32;

    public static String longToIp(long i) {
        long ipOnly = i & 0xFFFFFFFF;
        long netmask = (long) (i >> NUMBER_OF_BITS_IN_IP);
        StringBuffer result = new StringBuffer();
        result.append((ipOnly >> TWENTY_FOUR) & X_FF);
        result.append(DOT);//from   w w w  .  j av  a  2s. c om
        result.append((ipOnly >> SIXTEEN) & X_FF);
        result.append(DOT);
        result.append((ipOnly >> EIGHT) & X_FF);
        result.append(DOT);
        result.append(ipOnly & X_FF);
        String fullIpAddress = "";
        if (netmask > 0) {
            fullIpAddress = result.toString() + SLASH + slashnetFromAddrs(netmask);
        } else
            fullIpAddress = result.toString();

        return fullIpAddress;
    }

    public static int slashnetFromAddrs(long addrs) {
        return (int) (SLASHNET_VALUE - log2(addrs));
    }

    private static double log2(double d) {
        return Math.log(d) / Math.log(2.0);
    }
}

Related

  1. long2IP(long ipLong)
  2. long2IP(long proper_address)
  3. long2IpAdress(long src)
  4. longToIp(long address)
  5. longToIp(long i)
  6. longToIp(long ip)
  7. longToIp(long ip)
  8. LongToIp(long longIp)
  9. longToIp(long longValue)