在普通的旧VB6中,以下.NET代码的等价物是什么?
byte[] reversedContents = fileContents.Reverse().ToArray();
答案 0 :(得分:2)
您可以使用for
循环
For i = 0 To UBound(a) \ 2
k = a(i) : a(i) = a(UBound(a) - i) : a(UBound(a) - i) = k
Next i
答案 1 :(得分:2)
如果我们可以假设一个动态字节数组和一个非DBCS语言环境,那么这通常很快(以及一个单行):
Bytes = StrConv(StrReverse(StrConv(Bytes, vbUnicode)), vbFromUnicode)
即使不快,也不会慢得多。但是,当数组很短时, 会慢。
时间比较试验台:
Option Explicit
Private Sub Easy()
Dim Bytes() As Byte
Dim Iterate As Long
Bytes = StrConv("abcdefghijklmnopqrstuvwxyz", vbFromUnicode)
For Iterate = 1 To 100000
Bytes = StrConv(StrReverse(StrConv(Bytes, vbUnicode)), vbFromUnicode)
Next
End Sub
Private Sub Hard()
Dim Bytes() As Byte
Dim Iterate As Long
Dim I As Long
Dim B As Byte
Bytes = StrConv("abcdefghijklmnopqrstuvwxyz", vbFromUnicode)
For Iterate = 1 To 100000
For I = 0 To UBound(Bytes) \ 2
B = Bytes(I)
Bytes(I) = Bytes(UBound(Bytes) - I)
Bytes(UBound(Bytes) - I) = B
Next
Next
End Sub
Private Sub Main()
Easy
Hard
End Sub
但总的来说,"硬"方法更安全。
答案 2 :(得分:0)
其他答案对我有帮助,但我发现我的数组是使用返回基于1的数组的自定义Split例程构建的。因此,假设阵列基于0是没有用的。此例程可以反转基于0或基于1的数组。我意识到最初的问题是反转字节数组,但同样的逻辑将适用。
Private Sub Reverse(ByRef s() As String, Optional ByVal base As Integer = 0)
Dim i As Long
Dim sSwap As String
For i = base To (UBound(s) + base) \ 2
sSwap = s(i)
s(i) = s((UBound(s) + base) - i)
s((UBound(s) + base) - i) = sSwap
Next
End Sub