把一个IP地址转成16进制的数字该怎么转

http://www.xishuiw.com 2008年07月05日20:40 浠水网

'转换过程如下:
'1、取得IP,如:192.168.0.1
'2、再将IP顺序反转:1.0.168.192
'3、将IP分成四部分,分别转成16进制,位数不足,在左边加0,转换完成后为:01.00.A8.C0
'4、去掉所有的".",结果为:100A8C0
'5、再将其转成十进制:16820416

'// IP到数字转换
Public Function IPToNumber(ByVal strValue As String) As String
Dim strTemp As String
Dim strIP() As String
Dim strOutput As String
Dim i As Integer

On Error GoTo LocatErr

'// 将IP分成4个部份
strIP = Split(strValue, ".")

'// 这里是将IP倒过来的,即:192.168.0.1=1.0.168.192
For i = 3 To 0 Step -1
strTemp = Hex$(strIP(i))
'// 将每部份以2位数的数字表示
strOutput = strOutput & String(2 - Len(strTemp), "0") & strTemp
Next

IPToNumber = CStr(Trim(CLng("&H" & strOutput)))

Exit Function
LocatErr:
Err.Clear
IPToNumber = ""
End Function

'// 数字到IP转换(是通过 IPToNumber 转换过的数字)
Public Function NumberToIP(ByVal strValue As String) As String
Dim strTemp As String
Dim strIP(1 To 4) As String
Dim strOutput As String
Dim i As Integer

On Error GoTo LocatErr

'// 取得十六进制值
strTemp = Hex$(strValue)

'// 不足8位前面加 0
strTemp = String(8 - Len(strTemp), "0") & strTemp

'// 将数字以每2位分给数组
For i = 1 To 4
strIP(i) = Mid$(strTemp, (i - 1) * 2 + 1, 2)
Next

'// 合并IP
For i = 4 To 1 Step -1
strOutput = strOutput & CInt("&H" & strIP(i)) & "."
Next

'// 去掉最后的一个 "."
strOutput = Left$(strOutput, Len(strOutput) - 1)

NumberToIP = strOutput
Exit Function
LocatErr:
Err.Clear
NumberToIP = ""
End Function
发表评论
上一篇手工更新策略gpupdate /Force
下一篇使用CDN后取真实IP地址的方法
正在加载中……