[英]Get bytes from array function in VB.NET
I need to get the bytes from an array of bytes starting at a certain index and for a certain length (4). 我需要从某个索引开始并具有一定长度(4)的字节数组中获取字节。 How can I get this? 我怎么能得到这个?
Note: I don't want to use the Array.copy sub as it is not a function. 注意:我不想使用Array.copy子项,因为它不是函数。 I need to put it in something like Sub MySub(
[argument as byte()] the_function_I_Need(Array, index, length))
. 我需要将它放在Sub MySub(
[作为byte()的参数] the_function_I_Need(Array, index, length))
。
Something like: 就像是:
Dim portion As Byte() = New Byte(length - 1) {}
Array.Copy(originalArray, index, portion, 0, length)
The "- 1" is because of VB taking the last element index rather than the size. “ -1”是因为VB采用了最后一个元素索引而不是大小。
EDIT: I missed the bit about not wanting to use Array.Copy
. 编辑:我错过了关于不想使用Array.Copy
。 (Was it there when I posted the answer, or did you edit it in within the five minutes "grace period"?) (我发布答案时是在这里吗,还是您在“宽限期”的五分钟之内编辑了它?)
Just wrap this into a method if you really need to. 如果确实需要,只需将其包装为一个方法。 While there are alternatives using LINQ etc, this will be the most efficient way of doing it if you genuinely want a new array. 尽管有使用LINQ等的替代方法,但是如果您确实想要一个新的阵列,这将是最有效的方法。
There's also ArraySegment(Of T)
if you're happy to use a wrapper around the existing array - but that's not the same thing. 如果您愿意在现有数组周围使用包装器,则还可以使用ArraySegment(Of T)
-但这不是一回事。
Private Function the_function_you_need(ByVal arr As Byte(), ByVal ix As Integer, _
ByVal len As Integer) As Byte()
Dim arr2 As Byte() = New Byte(len - 1)
Array.Copy(arr, ix, arr2, 0, len)
Return arr2
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.