'List(Of Byte())' 到 'Byte()' 在 VB.NET 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14961261/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:23:52  来源:igfitidea点击:

'List(Of Byte())' to 'Byte()' in VB.NET

vb.net

提问by Shahriyar

I have a list of bytes:

我有一个字节列表:

Public Function GenPackets()
    Dim Stream As NetworkStream = ConnectionSocket.GetStream()
    Dim DataList As New List(Of Byte())
    Dim Data As Byte()
    DataList.Add(IntegerToByte(My.Settings.BotUID))

    ' TO DO       Stream.Write(Data, 0, 3)
    Return Data
End Function

How can I convert DataListand all of its bytes to a single Byte() for use in Stream.Write?

如何将DataList其所有字节转换为单个 Byte() 以供使用Stream.Write

回答by Konrad Rudolph

No need to convert your data, just iterate over the list:

无需转换您的数据,只需遍历列表:

For Each buffer As Byte() In Datalist
    yourStream.Write(buffer)
Next

This is vastly more efficient than first concatenating all the individual arrays to create one big array.

这比首先连接所有单个阵列以创建一个大阵列要高效得多。

回答by Olivier Jacot-Descombes

As Konrad Rudolph already wrote, you don't need to flatten the data in order to write it to your stream.

正如康拉德·鲁道夫 (Konrad Rudolph) 已经写过的那样,您无需将数据展平即可将其写入您的流中。

For the sake of completeness, you can flatten your data list like this

为了完整起见,您可以像这样展平数据列表

Dim dataList As New List(Of Byte())
Dim data As Byte()

data = dataList.SelectMany(Function(x) x).ToArray()

Or with the LINQ syntax

或者使用 LINQ 语法

data = (From bytes In dataList From x In bytes Select x).ToArray()

回答by SysDragon

You could do the Listof Bytes instead of arrays:

您可以使用ListBytes 而不是数组:

Dim DataList As New List(Of Byte)
DataList.AddRange(IntegerToByte(My.Settings.BotUID))

Data = DataList.ToArray()