Click here to Skip to main content
15,352,974 members
Articles / Web Development / XHTML
Posted 4 Aug 2008


168 bookmarked

How to convert IP address to country name

Rate me:
Please Sign up or sign in to vote.
4.53/5 (69 votes)
14 Aug 2008CPOL2 min read
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("HTTP_X_FORWARDED_FOR") 
If nowip = "" Then
   nowip = Request.ServerVariables("REMOTE_ADDR")
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)


IP Address = w.x.y.z

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

IP Address =
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 "" 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:


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
"0033996344","0033996351","GB","GBR","UNITED KINGDOM"
"0050331648","0083886079","US","USA","UNITED STATES"
FieldData TypeField Description
IP_FROMNUMERICAL (DOUBLE)Beginning of the IP address range.
IP_TONUMERICAL (DOUBLE)Ending of the IP address range.
COUNTRY_CODE2CHAR(2)Two-character country code based on ISO 3166.
COUNTRY_CODE3CHAR(3)Three-character country code based on ISO 3166.
COUNTRY_NAMEVARCHAR(50)Country name based on ISO 3166

Download CSV database

Convert to Access database

Image 1

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("HTTP_X_FORWARDED_FOR")
        If nowip = "" Then
            nowip = Request.ServerVariables("REMOTE_ADDR")
        End If

        If txtIPAddress.Text = "" Then
            txtIPAddress.Text = nowip
        End If
        lblError.Text = ""
    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

        dottedip = txtIPAddress.Text

            For i = 1 To 4
                pos = InStr(PrevPos + 1, dottedip, ".", 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

        txtIPNumber.Text = Dot2LongIP

        lblError.Text = Err.Description
    End Sub
End Class

Here is the output:



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

BugDownload links not working properly Pin
Member 124999823-May-16 2:26
MemberMember 124999823-May-16 2:26 
GeneralMy vote of 3 Pin
Josep Balague3-Oct-14 22:47
professionalJosep Balague3-Oct-14 22:47 
QuestionTHANKS! Pin
tedkrapf1-Jul-13 11:47
Membertedkrapf1-Jul-13 11:47 
GeneralMy vote of 5 Pin
Kunal Chowdhury «IN»21-Apr-11 2:32
professionalKunal Chowdhury «IN»21-Apr-11 2:32 
Generala copy of.......... Pin
Аslam Iqbal25-Mar-11 9:52
professionalАslam Iqbal25-Mar-11 9:52 
GeneralMy vote of 3 Pin
nvijin27-Nov-10 23:26
Membernvijin27-Nov-10 23:26 
GeneralMy vote of 1 PinPopular
Toddonian15-Dec-09 7:44
MemberToddonian15-Dec-09 7:44 
GeneralWork perfectly in C#, Thank you!! Pin
MEhran.NET19-Sep-09 9:03
MemberMEhran.NET19-Sep-09 9:03 
GeneralVB.Net Intergate Pin
Martin Houstrup17-Aug-09 5:22
MemberMartin Houstrup17-Aug-09 5:22 
Questiondoubt with the above Pin
svknair20-Jan-09 1:04
Membersvknair20-Jan-09 1:04 
AnswerRe: doubt with the above Pin
davidberlin10-Mar-09 3:43
Memberdavidberlin10-Mar-09 3:43 
GeneralThe world changes and Internet even faster. Pin
zeek22-Dec-08 8:08
Memberzeek22-Dec-08 8:08 
QuestionWhich Country is this Pin
khemant041115-Dec-08 20:51
Memberkhemant041115-Dec-08 20:51 
QuestionDatabase for Ip address Pin
desigank12-Nov-08 20:24
Memberdesigank12-Nov-08 20:24 
Generalnot simple. Use shifts or.. Pin
noname-201317-Oct-08 7:42
Membernoname-201317-Oct-08 7:42 
GeneralMe da este error. Pin
Member 173994428-Aug-08 7:32
MemberMember 173994428-Aug-08 7:32 
GeneralVery useful Pin
jsreekumar22-Aug-08 12:07
Memberjsreekumar22-Aug-08 12:07 
GeneralCool Pin
Ashutosh Phoujdar19-Aug-08 3:23
MemberAshutosh Phoujdar19-Aug-08 3:23 
GeneralWow! Pin
AbhilashAshok13-Aug-08 20:12
MemberAbhilashAshok13-Aug-08 20:12 
GeneralVery nice Pin
Petros Amiridis11-Aug-08 23:00
MemberPetros Amiridis11-Aug-08 23:00 
Generalthanks Pin
Member 404680211-Aug-08 22:47
MemberMember 404680211-Aug-08 22:47 
QuestionCity codes too? Pin
Wayne Hetherington11-Aug-08 13:01
MemberWayne Hetherington11-Aug-08 13:01 
GeneralInteresting Pin
kornakar5-Aug-08 20:38
Memberkornakar5-Aug-08 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.