Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Convert Ipv4 Address To Long.

form

  • Please log in to reply
1 reply to this topic

#1 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 13 April 2012 - 10:00 PM

This is something I needed for a project. I figured it might be useful to someone.

The formula for converting an IPV4 address to its value in long, is as follows:
(first octet * 256^3) + (second octet * 256^2) + (third octet * 256) + (fourth octet)


public static Long ipToLong(String ip) throws IllegalArgumentException {

		//the exception we're going to throw IF you input something invalid.
		IllegalArgumentException ex = new IllegalArgumentException("Invalid IP address " + ip);

		//create a Strin array of each octet, using "." as the delimiter.
		String[] octets = ip.split(Pattern.quote("."));

		//if the array is != 4, you did input an invalid IP address, so just throw the IllegalArgumentException.
		if (octets.length == 4) {

			//Lopp through the arry and check to make sure you didn't input an octet greater than 255, thus invalidating the IP.
			for (String s : octets) {
				if (Integer.parseInt(s) > 255) //You input an invalid address, so throw ex.
					throw ex;
			}

			//everything is good, so we do our calculations and return our long.
			long oct1 = Integer.parseInt(octets[0]) * (long)Math.pow(256,3);
			long oct2 = Integer.parseInt(octets[1]) * (long)Math.pow(256,2);
			long oct3 = Integer.parseInt(octets[2]) * 256;
			long oct4 = Integer.parseInt(octets[3]);
			return oct1 + oct2 + oct3 + oct4;
		}
		else
			throw ex; //You input and invalid address, because it was < or > 4 octets, making your String[] < or > 4.
	}

Edited by Alexander, 13 April 2012 - 10:34 PM.
Corrected formatting issues

  • 0

Averageloser.com - I used to be a programmer like you, then I took a -> in the knee. 


#2 Alexander

Alexander

    YOL9!

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh!
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Delphi/Object Pascal, Visual Basic .NET, Assembly, Fortran, Bash, Others

Posted 13 April 2012 - 10:39 PM

If the language supports bit level manipulation of integers, you can do this too with much lower processing power (raising to powers or multiplication is poor performance comparatively*):

long = (a << 24) + (b << 16) + (c << 8) + d;

It is unaffected by byte order.

*Optimised languages may do this automatically. Processor architecture may worsen or lighten the blow if not. Memory is certainly less abused in the above code, however.

Alexander.
  • 1

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.






Also tagged with one or more of these keywords: form

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download