Convert the IP address into an integer. - Android Network

Android examples for Network:IP Address

Description

Convert the IP address into an integer.

Demo Code


//package com.java2s;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    /**/*  w  w w.  j a v  a2s  .  c o m*/
     * Convert the IP address into an integer.
     *
     * @param ipAddress The IP address to convert.
     * @return The IP address converted as an integer.
     */
    private static int lookupHost(String ipAddress) {
        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getByName(ipAddress);
        } catch (UnknownHostException e) {
            return -1;
        }

        byte[] addressBytes;
        int address;
        addressBytes = inetAddress.getAddress();
        address = ((addressBytes[3] & 0xff) << 24)
                | ((addressBytes[2] & 0xff) << 16)
                | ((addressBytes[1] & 0xff) << 8)
                | (addressBytes[0] & 0xff);
        return address;
    }
}

Related Tutorials