分享最近写的一个IP地址与INT数据之间相互转换的函数,注意,该函数仅支持IPv4协议,不支持Ipv6。
下面的函数支持输出为INT或UINT类型,但实际上IP存储在INT和UINT内的数据在内存里的结构都是一样的,不过貌似SQL只支持INT结构的数据库,当然,请根据你自己的使用目的来选择函数。
Imports System.Text.RegularExpressions
Imports System.Net
#Region "IP与INT互转"
''' 将ip地址转换为int
Shared Function m_ip2int(ByVal ipads As String) As Integer
Dim ret As Integer = 0, b As Integer = 0
Dim mc As Match = Regex.Match(ipads, "(\d+)\.(\d+)\.(\d+)\.(\d+)")
If Not mc.Success Then Return 0
For i As Integer = 1 To 4
ret <<&#61; 8
b &#61; mc.Groups(i).Value
If b > Byte.MaxValue Then Return 0
ret &#43;&#61; b
Next
Return ret
End Function
Shared Function m_ip2int(ByVal ipads As IPAddress) As Integer
Dim bs As Byte() &#61; ipads.GetAddressBytes
If bs.Length <> 4 Then Return 0
Dim ret As Integer &#61; 0
For Each i As Byte In bs
ret <<&#61; 8
ret &#43;&#61; i
Next
Return ret
End Function
&#39;&#39;&#39; 将int转换为ip地址
Shared Function m_int2ip(ByVal ipint As Integer) As String
Dim ret(3) As Byte
Dim b As Integer &#61; 255
For i As Integer &#61; 0 To 3
ret(i) &#61; ipint And b
ipint >>&#61; 8
Next
Return String.Format("{3}.{2}.{1}.{0}", ret(0), ret(1), ret(2), ret(3))
End Function
&#39;&#39;&#39; 将ip地址转换为uint
Shared Function m_ip2uint(ByVal ip As String) As UInteger
Dim bs As Byte() &#61; IPAddress.Parse(ip).GetAddressBytes
Return CUInt(bs(3)) &#43; (CUInt(bs(2)) << 8) &#43; (CUInt(bs(1)) << 16) &#43; (CUInt(bs(0)) << 24)
End Function
&#39;&#39;&#39; 将uint转换为ip地址
Shared Function m_uint2ip(ByVal ip As UInteger) As String
Return New IPAddress(ip).ToString
End Function
&#39;&#39;&#39; 将ip地址转换为int
Shared Function m_IpToUInt(ByVal ip As String) As UInteger
Dim bs As Byte() &#61; IPAddress.Parse(ip).GetAddressBytes
Return CUInt(bs(3)) &#43; (CUInt(bs(2)) << 8) &#43; (CUInt(bs(1)) << 16) &#43; (CUInt(bs(0)) << 24)
End Function
&#39;&#39;&#39; 将uint转换为ip地址
Shared Function m_UIntToIP(ByVal ip As UInteger) As String
Return New IPAddress(ip).ToString
End Function
#End Region
Imports System.Net
#Region "IP与INT互转"
&#39;&#39;&#39;
Shared Function m_ip2int(ByVal ipads As String) As Integer
Dim ret As Integer &#61; 0, b As Integer &#61; 0
Dim mc As Match &#61; Regex.Match(ipads, "(\d&#43;)\.(\d&#43;)\.(\d&#43;)\.(\d&#43;)")
If Not mc.Success Then Return 0
For i As Integer &#61; 1 To 4
ret <<&#61; 8
b &#61; mc.Groups(i).Value
If b > Byte.MaxValue Then Return 0
ret &#43;&#61; b
Next
Return ret
End Function
Shared Function m_ip2int(ByVal ipads As IPAddress) As Integer
Dim bs As Byte() &#61; ipads.GetAddressBytes
If bs.Length <> 4 Then Return 0
Dim ret As Integer &#61; 0
For Each i As Byte In bs
ret <<&#61; 8
ret &#43;&#61; i
Next
Return ret
End Function
&#39;&#39;&#39;
Shared Function m_int2ip(ByVal ipint As Integer) As String
Dim ret(3) As Byte
Dim b As Integer &#61; 255
For i As Integer &#61; 0 To 3
ret(i) &#61; ipint And b
ipint >>&#61; 8
Next
Return String.Format("{3}.{2}.{1}.{0}", ret(0), ret(1), ret(2), ret(3))
End Function
&#39;&#39;&#39;
Shared Function m_ip2uint(ByVal ip As String) As UInteger
Dim bs As Byte() &#61; IPAddress.Parse(ip).GetAddressBytes
Return CUInt(bs(3)) &#43; (CUInt(bs(2)) << 8) &#43; (CUInt(bs(1)) << 16) &#43; (CUInt(bs(0)) << 24)
End Function
&#39;&#39;&#39;
Shared Function m_uint2ip(ByVal ip As UInteger) As String
Return New IPAddress(ip).ToString
End Function
&#39;&#39;&#39;
Shared Function m_IpToUInt(ByVal ip As String) As UInteger
Dim bs As Byte() &#61; IPAddress.Parse(ip).GetAddressBytes
Return CUInt(bs(3)) &#43; (CUInt(bs(2)) << 8) &#43; (CUInt(bs(1)) << 16) &#43; (CUInt(bs(0)) << 24)
End Function
&#39;&#39;&#39;
Shared Function m_UIntToIP(ByVal ip As UInteger) As String
Return New IPAddress(ip).ToString
End Function
#End Region