number To IP - Java Network

Java examples for Network:IP Address

Description

number To IP

Demo Code


import java.util.ArrayList;
import java.util.regex.Pattern;

public class Main{

    public static String numberToIP(long addr)
            throws InvalidIPAddressException {

        if (addr < 0L || addr > 4294967295L) {
            throw new InvalidIPAddressException("Invalid IP");
        }//from   w w  w  . j  av  a 2 s.  c o m

        StringBuilder ip = new StringBuilder();

        ip.append((addr >> 24) & 0xFF).append(".");
        ip.append((addr >> 16) & 0xFF).append(".");
        ip.append((addr >> 8) & 0xFF).append(".");
        ip.append((addr) & 0xFF);

        return ip.toString();
    }
}

Related Tutorials