Dim largehex As String, largedec As String, start As Long Function sums(ByVal x As String, ByVal y As String) As String Dim max As Long, temp As Long, i As Long, result As Variant max = IIf(Len(x) >= Len(y), Len(x), Len(y)) x = Right(String(max, "0") & x, max) y = Right(String(max, "0") & y, max) ReDim result(0 To max) For i = max To 1 Step -1 result(i) = Val(Mid(x, i, 1)) + Val(Mid(y, i, 1)) Next For i = max To 1 Step -1 temp = result(i) \ 10 result(i) = result(i) Mod 10 result(i - 1) = result(i - 1) + temp Next If result(0) = 0 Then result(0) = "" sums = Join(result, "") Erase resultEnd FunctionFunction multi(ByVal x As String, ByVal y As String) As String Dim result As Variant Dim xl As Long, yl As Long, temp As Long, i As Long xl = Len(Trim(x)) yl = Len(Trim(y))
ReDim result(1 To xl + yl) For i = 1 To xl For temp = 1 To yl result(i + temp) = result(i + temp) + Val(Mid(x, i, 1)) * Val(Mid(y, temp, 1)) Next NextFor i = xl + yl To 2 Step -1 temp = result(i) \ 10 result(i) = result(i) Mod 10 result(i - 1) = result(i - 1) + temp NextIf result(1) = "0" Then result(1) = "" multi = Join(result, "") Erase resultEnd Function Function POWER(ByVal x As Integer) As String POWER = 1 Dim i As Integer For i = 1 To x POWER = multi(POWER, CLng(&H1000000)) Next End Function Function HEXTODEC(ByVal x As String) As String Dim A() As String, i As Long, UNIT As Integer For i = 1 To Len(x) If Not IsNumeric("&h" & Mid(x, i, 1)) Then MsgBox "NOT A HEX FORMAT!", 64, "INFO": Exit Function Next x = String((6 - Len(x) Mod 6) Mod 6, "0") & xUNIT = Len(x) \ 6 - 1 ReDim A(UNIT) For i = 0 To UNIT A(i) = CLng("&h" & Mid(x, i * 6 + 1, 6)) Next For i = 0 To UNIT A(i) = multi(A(i), POWER(UNIT - i)) HEXTODEC = sums(HEXTODEC, A(i)) Next End Function 'hextodec Private Sub Command1_Click() largehex = "123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE" start = Timer largedec = HEXTODEC(largehex) MsgBox "hex:" & largehex & vbCrLf & vbCrLf & "dec:" & largedec, 64, "it take me about " & Format((Timer - start), "0.0000") & " seconds to trans the hugehex to dec" End Sub 'dectohex Private Sub Command2_Click() largedec = "717291632801918922030423142962255869416485831946154509020082328913819935586969956003869272100276828703758765759577310" start = Timer largehex = dectohex(largedec) MsgBox "dec:" & largedec & vbCrLf & vbCrLf & "hex:" & largehex, 64, "it take me about " & Format((Timer - start), "0.0000") & " seconds to trans the hugenum to hex"End SubFunction half(ByVal x As String) As String 'get half of x x = 0 & x Dim i As Long ReDim result(2 To Len(x)) As String For i = 2 To Len(x) result(i) = CStr(Val(Mid(x, i, 1)) \ 2 + IIf(Val(Mid(x, i - 1, 1)) Mod 2 = 1, 5, 0)) Next half = Join(result, "") If Left(half, 1) = "0" Then half = Right(half, Len(half) - 1) ' no zero ahead End Function Function dectohex(ByVal hugenum As String) As String ' trans hugenum to hex Do While Len(hugenum) > 2 dectohex = Hex(Val(Right(hugenum, 4)) Mod 16) & dectohex For i = 1 To 4 'devide hugenum by 16 hugenum = half(hugenum) Next Loop dectohex = Hex(Val(hugenum)) & dectohex End Function
我说兄台呀!你把10进制IP地址转成Long就得了。至于怎么转,我有三个办法。一会给你!
用什么方法你自己决定吧……Private Sub Command1_Click() Dim tBytes(0 To 3) As Byte tBytes(3) = 192 tBytes(2) = 168 tBytes(1) = 0 tBytes(0) = 11 Text1.Text = Hex(LongGetByBytesIP_1(tBytes())) End SubPrivate Sub Command2_Click() Dim tBytes(0 To 3) As Byte tBytes(3) = 192 tBytes(2) = 168 tBytes(1) = 0 tBytes(0) = 11 Text1.Text = Hex(LongGetByBytesIP_2(tBytes())) End SubPrivate Sub Command3_Click() Dim tBytes(0 To 3) As Byte tBytes(3) = 192 tBytes(2) = 168 tBytes(1) = 0 tBytes(0) = 11 Text1.Text = Hex(LongGetByBytesIP_3(tBytes())) End Sub Function LongGetByBytesIP_1(ByRef pBytes() As Byte) As Long '小仙妹现在用的方法。 Dim tOutLong As Long
CopyMemory tOutLong, pBytes(0), 4
LongGetByBytesIP_1 = tOutLong End FunctionFunction LongGetByBytesIP_2(ByRef pBytes() As Byte) As Long '小仙妹上高中时候用的方法。 Dim tOutLong As Long Dim tBytes() As Byte
Dim tSgn As Boolean
Dim tIndex As Long
tBytes() = pBytes()
tSgn = CBool(tBytes(3) \ 128)
tBytes(3) = tBytes(3) Mod 128
For tIndex = 3 To 0 Step -1 tOutLong = tOutLong + (tBytes(tIndex) * (256 ^ tIndex)) Next
If tSgn Then tOutLong = ((&H7FFFFFFF Xor tOutLong) + 1) * -1 End If
LongGetByBytesIP_2 = tOutLong End FunctionFunction LongGetByBytesIP_3(ByRef pBytes() As Byte) As Long '小仙妹初中时候用的方法。 Dim tOutLong As Long
Dim tIndex As Long Dim tHexStr As String Dim tHexLongStr As String
tHexLongStr = "&H"
For tIndex = 3 To 0 Step -1 tHexStr = Hex(pBytes(tIndex)) If Len(tHexStr) < 2 Then tHexStr = "0" & tHexStr tHexLongStr = tHexLongStr & tHexStr Next
tOutLong = CLng(tHexLongStr)
LongGetByBytesIP_3 = tOutLong End Function
非常抱歉,刚才看错了。你最终目的是想将long转换为Bytes()在上面代码基础上增下面的。Private Sub Command4_Click() Dim tLong As Double Dim tBytes() As Byte tLong = CLng(Text2.Text) tBytes() = BytesGetByLong_1(tLong) 'BytesViewToTextBox
BytesViewToTextBox tBytes(), Text3 End SubPrivate Sub Command5_Click() Dim tLong As Double Dim tBytes() As Byte tLong = CLng(Text2.Text) tBytes() = BytesGetByLong_2(tLong) 'BytesViewToTextBox
BytesViewToTextBox tBytes(), Text3 End SubFunction BytesGetByLong_1(ByVal pLong As Long) As Byte() Dim tOutBytes() As Byte
ReDim tOutBytes(0 To 3)
CopyMemory tOutBytes(0), pLong, 4
BytesGetByLong_1 = tOutBytes() End FunctionFunction BytesGetByLong_2(ByVal pLong As Long) As Byte() Dim tOutBytes() As Byte Dim tBytes_Length As Long
结果是:D39001B0
i = -745537104
Debug.Print Hex(i)
也不会出错的
Dim dec
dec = Trim(decnum.Text)
If dec < 0 Then
dec = dec + 2 ^ 32
End If
dec = Hex(dec)
MsgBox ("IP地址是" & dec)
在dec = Hex(dec)这句就overflow了
4294967295,你可以考虑将处理改为2 ^ 31
&H7fffffff=2147483647
&H8fffffff=-1879048193
Function sums(ByVal x As String, ByVal y As String) As String
Dim max As Long, temp As Long, i As Long, result As Variant
max = IIf(Len(x) >= Len(y), Len(x), Len(y))
x = Right(String(max, "0") & x, max)
y = Right(String(max, "0") & y, max)
ReDim result(0 To max)
For i = max To 1 Step -1
result(i) = Val(Mid(x, i, 1)) + Val(Mid(y, i, 1))
Next
For i = max To 1 Step -1
temp = result(i) \ 10
result(i) = result(i) Mod 10
result(i - 1) = result(i - 1) + temp
Next
If result(0) = 0 Then result(0) = ""
sums = Join(result, "")
Erase resultEnd FunctionFunction multi(ByVal x As String, ByVal y As String) As String
Dim result As Variant
Dim xl As Long, yl As Long, temp As Long, i As Long
xl = Len(Trim(x))
yl = Len(Trim(y))
ReDim result(1 To xl + yl)
For i = 1 To xl
For temp = 1 To yl
result(i + temp) = result(i + temp) + Val(Mid(x, i, 1)) * Val(Mid(y, temp, 1))
Next
NextFor i = xl + yl To 2 Step -1
temp = result(i) \ 10
result(i) = result(i) Mod 10
result(i - 1) = result(i - 1) + temp
NextIf result(1) = "0" Then result(1) = ""
multi = Join(result, "")
Erase resultEnd Function
Function POWER(ByVal x As Integer) As String
POWER = 1
Dim i As Integer
For i = 1 To x
POWER = multi(POWER, CLng(&H1000000))
Next
End Function
Function HEXTODEC(ByVal x As String) As String
Dim A() As String, i As Long, UNIT As Integer
For i = 1 To Len(x)
If Not IsNumeric("&h" & Mid(x, i, 1)) Then MsgBox "NOT A HEX FORMAT!", 64, "INFO": Exit Function
Next
x = String((6 - Len(x) Mod 6) Mod 6, "0") & xUNIT = Len(x) \ 6 - 1
ReDim A(UNIT)
For i = 0 To UNIT
A(i) = CLng("&h" & Mid(x, i * 6 + 1, 6))
Next
For i = 0 To UNIT
A(i) = multi(A(i), POWER(UNIT - i))
HEXTODEC = sums(HEXTODEC, A(i))
Next
End Function
'hextodec
Private Sub Command1_Click()
largehex = "123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE123456789ABCDE"
start = Timer
largedec = HEXTODEC(largehex)
MsgBox "hex:" & largehex & vbCrLf & vbCrLf & "dec:" & largedec, 64, "it take me about " & Format((Timer - start), "0.0000") & " seconds to trans the hugehex to dec"
End Sub
'dectohex
Private Sub Command2_Click()
largedec = "717291632801918922030423142962255869416485831946154509020082328913819935586969956003869272100276828703758765759577310"
start = Timer
largehex = dectohex(largedec)
MsgBox "dec:" & largedec & vbCrLf & vbCrLf & "hex:" & largehex, 64, "it take me about " & Format((Timer - start), "0.0000") & " seconds to trans the hugenum to hex"End SubFunction half(ByVal x As String) As String 'get half of x
x = 0 & x
Dim i As Long
ReDim result(2 To Len(x)) As String
For i = 2 To Len(x)
result(i) = CStr(Val(Mid(x, i, 1)) \ 2 + IIf(Val(Mid(x, i - 1, 1)) Mod 2 = 1, 5, 0))
Next
half = Join(result, "")
If Left(half, 1) = "0" Then half = Right(half, Len(half) - 1) ' no zero ahead
End Function
Function dectohex(ByVal hugenum As String) As String ' trans hugenum to hex
Do While Len(hugenum) > 2
dectohex = Hex(Val(Right(hugenum, 4)) Mod 16) & dectohex
For i = 1 To 4 'devide hugenum by 16
hugenum = half(hugenum)
Next
Loop
dectohex = Hex(Val(hugenum)) & dectohex
End Function
Dim tBytes(0 To 3) As Byte
tBytes(3) = 192
tBytes(2) = 168
tBytes(1) = 0
tBytes(0) = 11
Text1.Text = Hex(LongGetByBytesIP_1(tBytes()))
End SubPrivate Sub Command2_Click()
Dim tBytes(0 To 3) As Byte
tBytes(3) = 192
tBytes(2) = 168
tBytes(1) = 0
tBytes(0) = 11
Text1.Text = Hex(LongGetByBytesIP_2(tBytes()))
End SubPrivate Sub Command3_Click()
Dim tBytes(0 To 3) As Byte
tBytes(3) = 192
tBytes(2) = 168
tBytes(1) = 0
tBytes(0) = 11
Text1.Text = Hex(LongGetByBytesIP_3(tBytes()))
End Sub
Function LongGetByBytesIP_1(ByRef pBytes() As Byte) As Long
'小仙妹现在用的方法。
Dim tOutLong As Long
CopyMemory tOutLong, pBytes(0), 4
LongGetByBytesIP_1 = tOutLong
End FunctionFunction LongGetByBytesIP_2(ByRef pBytes() As Byte) As Long
'小仙妹上高中时候用的方法。
Dim tOutLong As Long
Dim tBytes() As Byte
Dim tSgn As Boolean
Dim tIndex As Long
tBytes() = pBytes()
tSgn = CBool(tBytes(3) \ 128)
tBytes(3) = tBytes(3) Mod 128
For tIndex = 3 To 0 Step -1
tOutLong = tOutLong + (tBytes(tIndex) * (256 ^ tIndex))
Next
If tSgn Then
tOutLong = ((&H7FFFFFFF Xor tOutLong) + 1) * -1
End If
LongGetByBytesIP_2 = tOutLong
End FunctionFunction LongGetByBytesIP_3(ByRef pBytes() As Byte) As Long
'小仙妹初中时候用的方法。
Dim tOutLong As Long
Dim tIndex As Long
Dim tHexStr As String
Dim tHexLongStr As String
tHexLongStr = "&H"
For tIndex = 3 To 0 Step -1
tHexStr = Hex(pBytes(tIndex))
If Len(tHexStr) < 2 Then tHexStr = "0" & tHexStr
tHexLongStr = tHexLongStr & tHexStr
Next
tOutLong = CLng(tHexLongStr)
LongGetByBytesIP_3 = tOutLong
End Function
Dim tLong As Double
Dim tBytes() As Byte tLong = CLng(Text2.Text)
tBytes() = BytesGetByLong_1(tLong) 'BytesViewToTextBox
BytesViewToTextBox tBytes(), Text3
End SubPrivate Sub Command5_Click()
Dim tLong As Double
Dim tBytes() As Byte tLong = CLng(Text2.Text)
tBytes() = BytesGetByLong_2(tLong) 'BytesViewToTextBox
BytesViewToTextBox tBytes(), Text3
End SubFunction BytesGetByLong_1(ByVal pLong As Long) As Byte()
Dim tOutBytes() As Byte
ReDim tOutBytes(0 To 3)
CopyMemory tOutBytes(0), pLong, 4
BytesGetByLong_1 = tOutBytes()
End FunctionFunction BytesGetByLong_2(ByVal pLong As Long) As Byte()
Dim tOutBytes() As Byte
Dim tBytes_Length As Long
Dim tIndex As Long
Dim tIndex_Over As Long
Dim tMinus As Boolean
Dim tLong As Long
tBytes_Length = Len(pLong) - 1
tIndex_Over = tBytes_Length - 1
tMinus = pLong < 0
tLong = pLong
If tMinus Then
tLong = (&H7FFFFFFF Xor (Abs(tLong) - 1))
End If
ReDim tOutBytes(tBytes_Length)
For tIndex = 0 To tIndex_Over
tOutBytes(tIndex) = (tLong \ (256 ^ tIndex)) Mod 256
Next
tOutBytes(tBytes_Length) = (tLong \ (256 ^ tBytes_Length)) Mod 256 + (tMinus And &H80)
BytesGetByLong_2 = tOutBytes()
End Function
http://expert.csdn.net/Expert/topicview.asp?id=2974472
我用系统的计算器转换后结果为D39001B0,这个结果长度为8,把它每两位两们分开来,正好4位,而FF正好是255,正是IP每一段的最大限制,那么你在代码里有这样一句:
MsgBox ("IP地址是" & dec)
也就是说D39001B0即为IP?
那么我想知道-745537104所对应的IP是否为:211.144.1.176?
如果不是,就不用往下面看了,下面的全是垃圾:)211 144 1 176
D3 90 01 B0本来我是打算看看怎么样可以把-745537104直接运算成211.144.1.176而不经过+2^32这一步骤,这样就不会出现不能用Hex函数转换3549430192到16进制的问题了
第一步当然是把-745537104转换成16进制看看,结果把我吓了一大跳,你看了也要跳的,呵呵
?hex(-745537104)
D39001B0
看看,竟然得到D39001B0?这是什么,这就是你要的IP?
难道hex(-745537104)=hex(3549430192)???
哈哈,这样是不是就可以跳过造成溢出的那一步啦,你想得到3549430192的16进制,就直接hex(-745537104)就可以了!!!然后我又做了次逆向运算
我用我此时的IP,218.92.90.213,把它变成16进制
218 = DA
92 = 5C
90 = 5A
213 = D5
?&HDA5C5AD5
-631481643
?-631481643+2^32
3663485653
用系统计算器转换3663485653到16进制,得到的正是DA5C5AD5!!!嘿嘿,目的达到,写代码:
Private Sub Command1_Click()
Dim num As Long, a(3) As Variant
num = -631481643
a(0) = ((num) And &HFF000000) \ &HFF0000 And &HFF
a(1) = ((num) And &HFF0000) \ &HFF00&
a(2) = ((num) And &HFF00&) \ &HFF
a(3) = (num) And &HFF
Debug.Print "IP为:" & Join(a, ".")
End Sub返回:
IP为:218.92.90.213嘿嘿,正是我的IP呀^o^再把代码中的num的值改为-745537104
返回:
IP为:211.144.1.176成了:)我花了那么些心思,楼主有没有赏啊,再开张帖子送我些分分?:)