Translation-convert an IP address to an IP number and obtain a country

Source: Internet
Author: User
Tags ip number

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 subject of the country. It should not be changed.ArticleTo download;

 

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.

About slave DatabaseCodeI won't talk much about it.

 

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

 

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.