Java IP address convert to long

Description

Java IP address convert to long


public class Main {
    public static void main(String[] argv) throws Exception {
        String ip = "127.0.0.1";
        System.out.println(ip2int(ip));
    }//ww  w  .  j a  va2s .  c  o m

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



PreviousNext

Related