How to Compare IP Addresses

Posted on : 17 Feb, 10:00 PM

One of my colleagues recently asked me, “How can I tell if an IP address is within a specific IP Range?” This question seemed to have an easy answer at first. But after taking a deeper look, I found that there are many possibilities to consider when determining if an IP is within a certain range.

The easiest way to determine the range is to convert the IP into a decimal format and then do the ordinary comparing statements necessary.

You can do this by assigning a weight for each part of the IP. Each part has a maximum value of 255. Thus, the best weight you can choose is 256. The last value of the IP address will have a weight of 256 to power 0, which is 1. The second part of the IP address should have a weight of 256 to power 1, which is 256 and so on.

So if you consider the IP address in the following format: A.B.C.D, then the equation will look like this:

D + (C * 256) + (B * 256 * 256) + (A * 256 * 256 * 256)

For example:

192.168.0.1 = 1 + (0 * 256) + (168 * 256 * 256) + (192 * 256 * 256 * 256) = 3232235521

From a code point of view, comparing numeric values is better and easier. After converting IPs to the numeric representation, your code will be very simple and you will only need to have single IF statement to know if the IP address is within a specific IP Range or not.

For a variety of reasons, you may want to know the IP address and range of individual users who access your system. For example, I was recently visiting www.eeye.com to download a product form. I noticed that they were providing the trial version of Blink software, but it was under different trial periods according to your location. They stated that if you were in the US/Canada you would have a 1 year trial, whereas if you were outside the US/Canada then you would have a trial version for only three months. They were able to determine user location by comparing each IP address to their IP address database. So as you can see, being able to determine an IP address and range can be a very useful capability to have.

Related Posts

No related posts found
;

How to Compare IP Addresses

Posted on : 17 Feb, 10:00 PM

One of my colleagues recently asked me, “How can I tell if an IP address is within a specific IP Range?” This question seemed to have an easy answer at first. But after taking a deeper look, I found that there are many possibilities to consider when determining if an IP is within a certain range.

The easiest way to determine the range is to convert the IP into a decimal format and then do the ordinary comparing statements necessary.

You can do this by assigning a weight for each part of the IP. Each part has a maximum value of 255. Thus, the best weight you can choose is 256. The last value of the IP address will have a weight of 256 to power 0, which is 1. The second part of the IP address should have a weight of 256 to power 1, which is 256 and so on.

So if you consider the IP address in the following format: A.B.C.D, then the equation will look like this:

D + (C * 256) + (B * 256 * 256) + (A * 256 * 256 * 256)

For example:

192.168.0.1 = 1 + (0 * 256) + (168 * 256 * 256) + (192 * 256 * 256 * 256) = 3232235521

From a code point of view, comparing numeric values is better and easier. After converting IPs to the numeric representation, your code will be very simple and you will only need to have single IF statement to know if the IP address is within a specific IP Range or not.

For a variety of reasons, you may want to know the IP address and range of individual users who access your system. For example, I was recently visiting www.eeye.com to download a product form. I noticed that they were providing the trial version of Blink software, but it was under different trial periods according to your location. They stated that if you were in the US/Canada you would have a 1 year trial, whereas if you were outside the US/Canada then you would have a trial version for only three months. They were able to determine user location by comparing each IP address to their IP address database. So as you can see, being able to determine an IP address and range can be a very useful capability to have.

Related Posts

No related posts found
;

footer-img

Integrant’s Vision is to transform the software development lifecycle through predictable results.

Subscribe

To get our newsletter & stay updated

© 2023 Integrant, Inc. All Rights Reserved | Privacy