7,763
社区成员
发帖
与我相关
我的任务
分享
Dim a() As Byte, b() As Byte
ReDim a(2)
a(0) = 2: a(1) = 1: a(2) = 1
……这里通过某些操作使得 b 也拥有了3个有效元素,但内容随机
Sub GetBytes(souByte() As Byte, conByte() As Byte)
Dim LenSouByte As Long, LenConByte As Long
Dim i As Long, j As Long
Dim Length As Long
LenSouByte = UBound(souByte)
LenConByte = UBound(conByte)
For i = LenSouByte - LenConByte To LenSouByte
j = i
Length = 0
Do While (souByte(j) = conByte(Length))
j = j + 1
Length = Length + 1
If j > LenSouByte Then Exit For
Loop
Next i
ReDim Preserve souByte(LenSouByte + (LenConByte + 1 - Length))
j = LenConByte
For i = UBound(souByte) To LenSouByte + 1 Step -1
souByte(i) = conByte(j)
j = j - 1
Next
End Sub
'主函数
Sub GetBytes(souByte() As Byte, conByte() As Byte)
Dim LenSouByte As Long, LenConByte As Long
Dim i As Long, j As Long
Dim Length As Long
LenSouByte = UBound(souByte)
LenConByte = UBound(conByte)
For i = LenSouByte - LenConByte To LenSouByte
j = i
Length = 0
Do While (souByte(j) = conByte(Length)) And (j <= LenSouByte)
j = j + 1
Length = Length + 1
If j > LenSouByte Then Exit For
Loop
Next i
ReDim Preserve souByte(LenSouByte + (LenConByte + 1 - Length))
j = LenConByte
For i = UBound(souByte) To LenSouByte + 1 Step -1
souByte(i) = conByte(j)
j = j - 1
Next
End Sub
'Print结果
Sub PrintBytes(myByte() As Byte)
Dim i As Long
For i = 0 To UBound(myByte)
Debug.Print myByte(i);
Next
Debug.Print
End Sub
'测试
Private Sub Command1_Click()
Dim a() As Byte, b() As Byte
ReDim a(2)
a(0) = 3: a(1) = 2: a(2) = 1
ReDim b(2)
b(0) = 12: b(1) = 11: b(2) = 10
GetBytes b, a
PrintBytes b
ReDim b(2)
b(0) = 12: b(1) = 11: b(2) = 3
GetBytes b, a
PrintBytes b
ReDim b(2)
b(0) = 12: b(1) = 3: b(2) = 2
GetBytes b, a
PrintBytes b
ReDim b(2)
b(0) = 3: b(1) = 2: b(2) = 1
GetBytes b, a
PrintBytes b
End Sub