IP addresses converter

By Aion- on Nov 22, 2012

The first method, toLong(String), will convert the provided ip address to its long format. For instance, invoking toLong("255.255.255.255") will return 4294967295 whereas the second method, toString(long), will convert it back as "255.255.255.255", in other words, into a 32bit dotted format.

As both methods' access level modifiers are public and static, you needn't an instance of the class: you can invoke either method directly.

Examples
long ip = IPv4.toLong("255.255.255.255");
String ipAddress = IPv4.toString(ip);

/**
 * This utility provides methods to either convert an IPv4 address to its long format or a 32bit dotted format.
 *
 * @author Aion
 *         Created on 22/11/12
 */
public class IPv4 {

    /**
     * Returns the long format of the provided IP address.
     *
     * @param ipAddress the IP address
     * @return the long format of <code>ipAddress</code>
     * @throws IllegalArgumentException if <code>ipAddress</code> is invalid
     */
    public static long toLong(String ipAddress) {
        if (ipAddress == null || ipAddress.isEmpty()) {
            throw new IllegalArgumentException("ip address cannot be null or empty");
        }
        String[] octets = ipAddress.split(java.util.regex.Pattern.quote("."));
        if (octets.length != 4) {
            throw new IllegalArgumentException("invalid ip address");
        }
        long ip = 0;
        for (int i = 3; i >= 0; i--) {
            long octet = Long.parseLong(octets[3 - i]);
            if (octet > 255 || octet < 0) {
                throw new IllegalArgumentException("invalid ip address");
            }
            ip |= octet << (i * 8);
        }
        return ip;
    }

    /**
     * Returns the 32bit dotted format of the provided long ip.
     *
     * @param ip the long ip
     * @return the 32bit dotted format of <code>ip</code>
     * @throws IllegalArgumentException if <code>ip</code> is invalid
     */
    public static String toString(long ip) {
        // if ip is bigger than 255.255.255.255 or smaller than 0.0.0.0
        if (ip > 4294967295l || ip < 0) {
            throw new IllegalArgumentException("invalid ip");
        }
        StringBuilder ipAddress = new StringBuilder();
        for (int i = 3; i >= 0; i--) {
            int shift = i * 8;
            ipAddress.append((ip & (0xff << shift)) >> shift);
            if (i > 0) {
                ipAddress.append(".");
            }
        }
        return ipAddress.toString();
    }

}

Comments

Sign in to comment.
Aion-   -  Nov 25, 2012

Updated.

  • Renamed class name (Utility -> IPv4);
  • Renamed method ipToLong(String) as toLong(String);
  • Renamed method longToIp(long) as toString(long);
  • Added the throws tag to both methods to indicate an exception will be thrown if the given argument is invalid;
  • Changed description.
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.