Teneo !!!

Aaron’s blog on Networking, and Enterprise Technology

Java – IP Address to Integer and back

Many applications store the IP Address as an integer.  This makes it easy to perform mathematical operations on it (say, adding 1024 hosts to a subnet to get min/max addresses, etc).

Currently, I am implementing IPPlan to my toolset, to help me manage all my IP’s and subnets, and make it easy for others to request IP’s.  However, I didn’t want to duplicate the IP’s in use.  I wanted my current network management system to FEED the hosts it already knows into IPPlan.

Easy, I’ll just run a daily SQL to update the IPPlan database.  The issue is, my NMS stores the IP Address as a String, and IPPlan stores it as an integer.

So, taking bits and pieces off the internet, here are a couple of cool Java methods that helped me accomplish this:

From Limewire’s IP.java, here is converting an existing Integer to String:

public static String intToIp(int i) {
        return ((i >> 24 ) & 0xFF) + "." +
               ((i >> 16 ) & 0xFF) + "." +
               ((i >>  8 ) & 0xFF) + "." +
               ( i        & 0xFF);
    }

From Justin-Cook.com, here is converting an existing String to Integer:

public static Long ipToInt(String addr) {
        String[] addrArray = addr.split("\\.");

        long num = 0;
        for (int i=0;i<addrArray.length;i++) {
            int power = 3-i;

            num += ((Integer.parseInt(addrArray[i])%256 * Math.pow(256,power)));
        }
        return num;
    }

7 responses to “Java – IP Address to Integer and back

  1. try this February 25, 2010 at 4:25 am


    public static int ipToInt(final String addr) {
    final String[] addressBytes = addr.split("\\.");

    int ip = 0;
    for (int i = 0; i < 4; i++) {
    ip <<= 8;
    ip |= Integer.parseInt(addressBytes[i]);
    }
    return ip;
    }

  2. Robin March 24, 2010 at 10:49 pm

    Thanks U, this should save my night.

  3. Cristian June 18, 2010 at 7:59 am

    Pretty nice! Android gives you the IP address as integers so this is so useful!

  4. Michael February 24, 2011 at 1:02 am

    Be careful, though. None of these methods work properly
    when dealing with IPv6 addresses. Everyone needs to get on board
    with IPv6 now or else suffer major code reworking later on. I’m
    currently trying to do IP address string -> integer
    conversion at the moment, and I need to be able to handle both v4
    and v6 addresses. I haven’t found a solution yet.

    • Alok May 5, 2011 at 7:54 pm

      I m using K-means clustering of web log data…i have to use IP address and web log data as input to d algorithm and convert it into clusters for cache replacement….plz tell me a code to convert the ip address and web address into integers ,so that i can put it as input for clustering….. Mail me at aloksalian@yahoo.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: