Java IP Address to Long ipTolong(String address)

Here you can find the source of ipTolong(String address)

Description

Get IP Address in Long from String.

License

Open Source License

Parameter

Parameter Description
address IP Address that to be converted to long

Return

Long value of the IP Address

Declaration

private static long ipTolong(String address) 

Method Source Code

//package com.java2s;
/*//from  w  w w .j  a  va  2  s . c  om
 * Copyright ? 2016, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Get IP Address in Long from String.
     *
     * @param address IP Address that to be converted to long
     * @return Long value of the IP Address
     */
    private static long ipTolong(String address) {

        // Parse IP parts into an int array
        long[] ip = new long[4];
        String[] parts = address.split("\\.");

        for (int i = 0; i < 4; i++) {
            ip[i] = Long.parseLong(parts[i]);
        }
        // Add the above IP parts into an int number representing your IP
        // in a 32-bit binary form
        long ipNumbers = 0;
        for (int i = 0; i < 4; i++) {
            ipNumbers += ip[i] << (24 - (8 * i));
        }
        return ipNumbers;

    }
}

Related

  1. ip2Long(String ipAddress)
  2. ipToLong(byte[] address)
  3. ipToLong(byte[] octets)
  4. ipToLong(final String addr)
  5. ipToLong(String addr)
  6. ipToLong(String ip)
  7. ipToLong(String ip)
  8. ipToLong(String ip)
  9. ipToLong(String ip)