首页 行业资讯 宠物日常 宠物养护 宠物健康 宠物故事

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

发布网友 发布时间:2022-04-24 14:42

我来回答

1个回答

热心网友 时间:2022-05-03 05:56

'转换过程如下:
'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

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com