'
' IP地址点分表示法转换为十进制表示法
'
Public Shared Function Dot2LongIP(ByVal dotIP As String) As Long
' 使用正则表达式进行IP地址检验
Dim subIP As String() = Split(dotIP, ".")
'IP Address = w.x.y.z
'IP Number = 16777216 * w + 65536 * x + 256 * y + z
Dot2LongIP = 16777216 * CLng(subIP(0)) + 65536 * CLng(subIP(1)) + 256 * CLng(subIP(2)) + CLng(subIP(3))
End Function
Public Shared Function LongIP2Dot(ByVal longIP As Long) As String
'IP Address = w.x.y.z
'IP Number = 16777216 * w + 65536 * x + 256 * y + z
'w = int ( IP Number / 16777216 ) % 256
'x = int ( IP Number / 65536 ) % 256
'y = int ( IP Number / 256 ) % 256
'z = int ( IP Number ) % 256
Dim dotIP As String
Dim subIP As Integer
subIP = CInt(Fix(longIP / 16777216)) Mod 256
dotIP = CStr(subIP) + "."
subIP = CInt(Fix(longIP / 65536)) Mod 256
dotIP += CStr(subIP) + "."
subIP = CInt(Fix(longIP / 256)) Mod 256
dotIP += CStr(subIP) + "."
subIP = CInt(Fix(longIP Mod 256))
dotIP += CStr(subIP)
Return dotIP
End Function
' IP地址点分表示法转换为十进制表示法
'
Public Shared Function Dot2LongIP(ByVal dotIP As String) As Long
' 使用正则表达式进行IP地址检验
Dim subIP As String() = Split(dotIP, ".")
'IP Address = w.x.y.z
'IP Number = 16777216 * w + 65536 * x + 256 * y + z
Dot2LongIP = 16777216 * CLng(subIP(0)) + 65536 * CLng(subIP(1)) + 256 * CLng(subIP(2)) + CLng(subIP(3))
End Function
Public Shared Function LongIP2Dot(ByVal longIP As Long) As String
'IP Address = w.x.y.z
'IP Number = 16777216 * w + 65536 * x + 256 * y + z
'w = int ( IP Number / 16777216 ) % 256
'x = int ( IP Number / 65536 ) % 256
'y = int ( IP Number / 256 ) % 256
'z = int ( IP Number ) % 256
Dim dotIP As String
Dim subIP As Integer
subIP = CInt(Fix(longIP / 16777216)) Mod 256
dotIP = CStr(subIP) + "."
subIP = CInt(Fix(longIP / 65536)) Mod 256
dotIP += CStr(subIP) + "."
subIP = CInt(Fix(longIP / 256)) Mod 256
dotIP += CStr(subIP) + "."
subIP = CInt(Fix(longIP Mod 256))
dotIP += CStr(subIP)
Return dotIP
End Function
2005-05-12补充,Ninputer:的方法确实很好,效率肯定比前面的计算方式要高.
<StructLayout(LayoutKind.Explicit)> _
Public Structure IPConvert
<FieldOffset(0)> Public LongIP As Long
<FieldOffset(0)> Public DotIP0 As Byte
<FieldOffset(1)> Public DotIP1 As Byte
<FieldOffset(2)> Public DotIP2 As Byte
<FieldOffset(3)> Public DotIP3 As Byte
End Structure
Public Structure IPConvert
<FieldOffset(0)> Public LongIP As Long
<FieldOffset(0)> Public DotIP0 As Byte
<FieldOffset(1)> Public DotIP1 As Byte
<FieldOffset(2)> Public DotIP2 As Byte
<FieldOffset(3)> Public DotIP3 As Byte
End Structure