12,824,787 members (41,138 online)
alternative version

#### Stats

193.4K views
165 bookmarked
Posted 4 Aug 2008

# How to convert IP address to country name

, 14 Aug 2008 CPOL
 Rate this:
How do I retrieve the country name and country code from an IP number?

## How to retrieve a visitor's IP address

Every visitor to your site or web application has an IP address. It is quite handy to be able to get that address. It can be used for security logging, or perhaps tracing. It can also be used to determine where they are in the world, or at least where their ISP is.

The difficulty is when they are behind a proxy of some sort, and you can only see the IP address of the proxy server. So, here are the code snippets in ASP.NET that first check for an IP addresses that's forwarded from behind a proxy, and if there's none, then just gets the IP address. Here's the same IP retriever with proxy detection in .NET, but in VB.NET:

```Dim nowip As String
nowip = Request.ServerVariables(&quot;HTTP_X_FORWARDED_FOR&quot;)
If nowip = &quot;&quot; Then
End If```

## How do I convert an IP address to an IP number?

IP address (IPV4) is divided into four sub-blocks. Each sub-block has a different weight number, each powered by 256. The IP number is being used in the database because it is efficient to search between a range of numbers in the database.

The beginning IP number and the ending IP number are calculated based on the following formula:

`IP Number = 16777216*w + 65536*x + 256*y + z (1)`

where:

`IP Address = w.x.y.z`

For example, if the IP address is "202.186.13.4", then its IP number is "3401190660" based on the above formula.

```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```

To reverse the IP number to the IP address:

```w = int ( IP Number / 16777216 ) % 256
x = int ( IP Number / 65536 ) % 256
y = int ( IP Number / 256 ) % 256
z = int ( IP Number ) % 256```

where, `%` is the mod operator and `int` returns the integer part of the division.

## How do I retrieve the country name and the country code from the IP number?

Search the IP-country database to match a unique record that has the IP number that fits between the beginning IP number and the ending IP number.

For example, the IP address "202.186.13.4" is equivalent to the IP number "3401190660". It belongs to the following record in the database because it is between the beginning and the the ending of the IP number:

`&quot;3401056256&quot;,&quot;3401400319&quot;,&quot;MY&quot;,&quot;MALAYSIA&quot;`

From the recordset, the country name is Malaysia and the country code is MY. Here is a useful link.

## How do I use this database

### CSV file format

The CSV file contains four fields:

• Beginning of the IP address range
• Ending of the IP address range
• Two-character country code based on ISO 3166
• Three-character country code based on ISO 3166
• Country name based on ISO 3166
```&quot;0033996344&quot;,&quot;0033996351&quot;,&quot;GB&quot;,&quot;GBR&quot;,&quot;UNITED KINGDOM&quot;
&quot;0050331648&quot;,&quot;0083886079&quot;,&quot;US&quot;,&quot;USA&quot;,&quot;UNITED STATES&quot;
&quot;0094585424&quot;,&quot;0094585439&quot;,&quot;SE&quot;,&quot;SWE&quot;,&quot;SWEDEN&quot;```
 Field Data Type Field Description `IP_FROM` `NUMERICAL (DOUBLE)` Beginning of the IP address range. `IP_TO` `NUMERICAL (DOUBLE)` Ending of the IP address range. `COUNTRY_CODE2` `CHAR(2)` Two-character country code based on ISO 3166. `COUNTRY_CODE3` `CHAR(3)` Three-character country code based on ISO 3166. `COUNTRY_NAME` `VARCHAR(50)` Country name based on ISO 3166

### Convert to Access database

Here is the code-behind:

```Partial Class How_to_Convert_IP_Address_Country _
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles Me.Load
Dim nowip As String
nowip = Request.ServerVariables(&quot;HTTP_X_FORWARDED_FOR&quot;)
If nowip = &quot;&quot; Then
End If

End If
lblError.Text = &quot;&quot;
End Sub

Protected Sub Button1_Click(ByVal sender As Object,_
ByVal e As System.EventArgs) Handles Button1.Click
On Error GoTo HandleError

Dim dottedip As String
Dim Dot2LongIP As Double
Dim PrevPos As Double
Dim pos As Double
Dim num As Double

For i = 1 To 4
pos = InStr(PrevPos + 1, dottedip, &quot;.&quot;, 1)
If i = 4 Then
pos = Len(dottedip) + 1
End If
num = Int(Mid(dottedip, PrevPos + 1, pos - PrevPos - 1))
PrevPos = pos
Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
Next

txtIPNumber.Text = Dot2LongIP

HandleError:
lblError.Text = Err.Description
End Sub
End Class```

Here is the output:

## Share

 Web Developer United States
No Biography provided

## You may also be interested in...

 First Prev Next
 My vote of 3 jjjjjjbbbbbb3-Oct-14 23:47 jjjjjjbbbbbb 3-Oct-14 23:47
 THANKS! tedkrapf1-Jul-13 12:47 tedkrapf 1-Jul-13 12:47
 My vote of 5 Kunal_Chowdhury21-Apr-11 3:32 Kunal_Chowdhury 21-Apr-11 3:32
 a copy of.......... Аslam Iqbal25-Mar-11 10:52 Аslam Iqbal 25-Mar-11 10:52
 My vote of 3 nvijin28-Nov-10 0:26 nvijin 28-Nov-10 0:26
 good
 My vote of 1 Toddonian15-Dec-09 8:44 Toddonian 15-Dec-09 8:44
 Work perfectly in C#, Thank you!! mehran_o_net19-Sep-09 10:03 mehran_o_net 19-Sep-09 10:03
 VB.Net Intergate Martin Houstrup17-Aug-09 6:22 Martin Houstrup 17-Aug-09 6:22
 doubt with the above svknair20-Jan-09 2:04 svknair 20-Jan-09 2:04
 Re: doubt with the above davidberlin10-Mar-09 4:43 davidberlin 10-Mar-09 4:43
 The world changes and Internet even faster. zeek22-Dec-08 9:08 zeek 22-Dec-08 9:08
 Which Country is this khemant041115-Dec-08 21:51 khemant0411 15-Dec-08 21:51
 Database for Ip address desigank12-Nov-08 21:24 desigank 12-Nov-08 21:24
 not simple. Use shifts or.. ykorotia17-Oct-08 8:42 ykorotia 17-Oct-08 8:42
 Me da este error. Member 173994428-Aug-08 8:32 Member 1739944 28-Aug-08 8:32
 Very useful jsreekumar22-Aug-08 13:07 jsreekumar 22-Aug-08 13:07
 Cool ashu fouzdar19-Aug-08 4:23 ashu fouzdar 19-Aug-08 4:23
 Wow! AbhilashAshok13-Aug-08 21:12 AbhilashAshok 13-Aug-08 21:12
 Very nice Petros Amiridis12-Aug-08 0:00 Petros Amiridis 12-Aug-08 0:00
 thanks Member 404680211-Aug-08 23:47 Member 4046802 11-Aug-08 23:47
 City codes too? Wayne Hetherington11-Aug-08 14:01 Wayne Hetherington 11-Aug-08 14:01
 Interesting kornakar5-Aug-08 21:38 kornakar 5-Aug-08 21:38
 Last Visit: 31-Dec-99 19:00     Last Update: 28-Mar-17 1:18 Refresh 1