Convert the IP address to IP number and obtain the country

Source: Internet
Author: User
Tags ip number
Convert the IP address to IP number and obtain the country

Today, I have nothing to do at home. I went online and read an article about finding a country based on the IP address. In fact, I used one for my work, but I don't know how to do it.

Next, I will share the translation http://www.codeproject.com/kb/aspnet/aspxcode_net.aspxwith everyone;

 

Principle: The IP addresses in various countries now have an IP address begin number and an IP end number. The data starting from the country and ending data are stored in the database.

Finally, a calculation formula is used to convert a real IP address to an IP number, which is located between begin and end in the database. If so, it is in this country;

The IP number of each country should be the national standard, and it should not be changed. It will be provided for download after the article;

 

IP number calculation formula:

IP number = 16777216 * w + 65536 * x + 256 * Y + z (1)
Where
IP address = W. x. y. Z

 

 

 

 

 

 

 

Note: 16777216,65536, 256; this number is a coefficient, just like the circumference rate of 3.14

W. x. y. Z corresponds to the actual IP address, for example: 192.168.0.19; Link

 

Eg:

IP address = 202.186.13.4
So, w = 202, x = 186, y = 13 and Z = 4
IP number = 16777216*202 + 65536*186 + 256*13 + 4
= 3388997632 + 12189696 + 3328 + 4
= 3401190660

In this way, an IP number is obtained. Now we can go to our database to find the correct IP address segment, that is, the country where the IP address is located.

I will not talk much about the code from the database.

 

The key is to understand the principle and find the solution;

National Standard IP Range download: http://files.cnblogs.com/caodaiming/ip-to-country.zip

Article transferred from: http://www.cnblogs.com/caodaiming/archive/2008/08/09/1264233.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.