Java IP address convert to int

Description

Java IP address convert to int

public class Main {
    public static void main(String[] argv) throws Exception {
        String ip = "127.0.0.1";
        System.out.println(ip2int(ip));
    }/* w ww. ja va2  s.  co m*/

    public static long ip2int(String ip) {
        ip = ip.replace(".", ",");
        String[] items = ip.split(",");
        return Integer.valueOf(items[0]) << 24 | 
               Integer.valueOf(items[1]) << 16 |
               Integer.valueOf(items[2]) << 8  | 
               Integer.valueOf(items[3]);
    }
}



PreviousNext

Related