Java IP Address to Long convertIPS2Long(String ip)

Here you can find the source of convertIPS2Long(String ip)

Description

convert IPS Long

License

Apache License

Declaration

public static final long convertIPS2Long(String ip) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static final long convertIPS2Long(String ip) {
        char[] c = ip.toCharArray();
        byte[] b = { 0, 0, 0, 0 };
        for (int i = 0, j = 0; i < c.length;) {
            int d = (byte) (c[i] - '0');
            switch (c[i++]) {
            case '.':
                ++j;//from  ww w  .  j av  a  2  s .  c o  m
                break;
            default:
                if ((d < 0) || (d > 9))
                    return 0;
                if ((b[j] & 0xff) * 10 + d > 255)
                    return 0;
                b[j] = (byte) (b[j] * 10 + d);
            }
        }
        return 0x00000000ffffffffl & (b[0] << 24 | (b[1] & 0xff) << 16 | (b[2] & 0xff) << 8 | (b[3] & 0xff));
    }
}

Related

  1. convertIpString2Long(String IP)
  2. ip2Long(final String ip)
  3. ip2long(String ip)
  4. ip2long(String ip)