VB.NET 中的字节数组数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1987280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An array of array of bytes in VB.NET
提问by Jonathan.
I need an array and each item in the array is an array of bytes like this, but I'm not sure how to do the:
我需要一个数组,数组中的每个项目都是一个像这样的字节数组,但我不确定如何执行:
Dim xx as array
xx(0) *as byte* = {&H12, &HFF}
xx(1) *as byte* = {&H45, &HFE}
回答by SLaks
You can make a nested or "jagged" byte array like this:
您可以像这样制作嵌套或“锯齿状”字节数组:
Dim myBytes(6)() As Byte
This will create an empty array of 6 byte arrays. Each element in the outer array will be Nothing
until you assign an array to it, like this:
这将创建一个 6 字节数组的空数组。外部数组中的每个元素都将Nothing
在您为其分配数组之前,如下所示:
myBytes(0) = New Byte() { &H12, &Hff }
However, it would probably be a better idea to make a List
of byte arrays, like this:
但是,制作一个List
字节数组可能是一个更好的主意,如下所示:
Dim myBytes As New List(Of Byte())
This will create an empty list of byte array, which will stay empty until you put some byte arrays into it, like this:
这将创建一个字节数组的空列表,该列表将保持为空,直到您将一些字节数组放入其中,如下所示:
myBytes.Add(New Byte() { &H12, &Hff })
Unlike the nested array, a List(Of Byte())
will automatically expand to hold as many byte arrays as you put into it.
与嵌套数组不同, aList(Of Byte())
将自动扩展以容纳您放入其中的尽可能多的字节数组。
For more specific advice, please tell us what you're trying to do.
如需更具体的建议,请告诉我们您正在尝试做什么。
回答by Eilon
Please refer to this MSDN topicfor more details.
有关更多详细信息,请参阅此 MSDN 主题。
Here's the code to define a multidimensional array:
这是定义多维数组的代码:
Dim lotsaBytes(2,4) As Byte
And to initialize it:
并初始化它:
Dim lotsaBytes(,) As Byte = New Byte(2, 4) {{1, 2}, {3, 4}, {5, 6}, {7, 8}}
回答by imad
You can solve your problem with the following VB.NET example. Just drag and drop one button and one textbox. The code will be as follows inside the button click event:
您可以使用以下 VB.NET 示例解决您的问题。只需拖放一个按钮和一个文本框。按钮点击事件中的代码如下:
Private Sub btnCalcBcc_Click(sender As System.Object, e As System.EventArgs) Handles btnCalcBcc.Click
Dim BCC As Int16
Dim Bcc2 As Int16
Dim arr() As Byte = {&H1B, &H58, &H41, &H42, &H43, &H44, &H45, &H46, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H17, &H0, &H0, &H0, &H0}
For i As Integer = 0 To arr.Length - 1
BCC = BCC Xor arr(i)
BCC = BCC << 1
Bcc2 = (BCC >> 8)
Bcc2 = Bcc2 And &H1
BCC = BCC + Bcc2
Next
txtBCC.Text = BCC
End Sub