Return to Snippet

Revision: 68247
at December 17, 2014 20:50 by Hexahow


Initial Code
''' <summary>
''' Convert IPV6 Address to IP Number
''' Free geolocation database can be downloaded at:
''' http://lite.ip2location.com/
''' </summary>

Dim strIP As String = "2404:6800:4001:805::1006"
Dim address As System.Net.IPAddress
Dim ipnum As System.Numerics.BigInteger

If System.Net.IPAddress.TryParse(strIP, address) Then
	Dim addrBytes() As Byte = address.GetAddressBytes()

	If System.BitConverter.IsLittleEndian Then
		Dim byteList As New System.Collections.Generic.List(Of Byte)(addrBytes)
		byteList.Reverse()
		addrBytes = byteList.ToArray()
	End If

	If addrBytes.Length > 8 Then
		'IPv6
		ipnum = System.BitConverter.ToUInt64(addrBytes, 8)
		ipnum <<= 64
		ipnum += System.BitConverter.ToUInt64(addrBytes, 0)
	Else
		'IPv4
		ipnum = System.BitConverter.ToUInt32(addrBytes, 0)
	End If
End If

Initial URL
http://lite.ip2location.com/

Initial Description
Use the code below to convert the IP address of your web visitors and lookup for their geographical location, e.g. country, state, city, latitude/longitude, ZIPs, timezone and so on. Free database can be downloaded at http://lite.ip2location.com.

Initial Title
Convert IPv6 Address to IP numbers

Initial Tags
ip

Initial Language
VB.NET