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