会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
学无止境
好好学习,天天向上
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
IP地址点分表示法与十进制表示法的转换
'
'
IP地址点分表示法转换为十进制表示法
'
Public
Shared
Function Dot2LongIP()
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()
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
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
注意需要引入名称空间System.Runtime.InteropServices,并且LongIP的类型确实是Long,而不是Integer.
posted on
2004-10-28 10:57
zhumk
阅读(
5521
) 评论(
12
)
收藏
举报
刷新页面
返回顶部