在字节数组中转换列表列表

我正在尝试编写一种方法
List<string>

, 然后将整个列表转换为一个大量字节数。 类似于这个:


private byte[] ConvertStringsToBytes/List<string> list/
{
List<byte> byteList = new List<byte>//;

foreach /var i in list/
{
byteList.Add/Encoding.UTF8.GetBytes/i//;
}

return byteList.ToArray//;
}


但是,我得到:

参数类型 'byte[]' 无法分配给类型参数 'byte' 在
byteList. Add/Encoding.UTF8.GetBytes/i//;

我在哪里弄错了? 如何将此列表转换为单路径数组?
</byte></byte></string></string>
已邀请:

冰洋

赞同来自:

更有效的方法将首先将行连接在一起,然后将它们转换为类似于此的字节数组:


List<string> input = new List<string> { "first", "second" };
string fullString = String.Join/String.Empty, list.ToArray///;
byte[] byteArray = Encoding.UTF8.GetBytes/fullString/;


如果在此列表中有很多线条,您可以这样做:

Edit:

在基准测试之后,该方法比上述方法慢得多。


List<string> input = new List<string> { "first", "second" };
StringBuilder sb = new StringBuilder//;
foreach /string s in input /
sb.Append/s/;

byte[] byteArray = Encoding.UTF8.GetBytes/sb.ToString///;


Edit:

在这篇文章中提到了一些基准测试。 以下是释放组件的输出:


ConvertWithString 896ms
ConvertWithStringBuilder 858ms
ConvertWithConcat 1529ms
ConvertWithSelectMany 2234ms
ConvertWithBuffer 904ms

ConvertWithString 501ms
ConvertWithStringBuilder 919ms
ConvertWithConcat 1435ms
ConvertWithSelectMany 2044ms
ConvertWithBuffer 636ms


如果你没有,似乎表现真的没关系

很多

学期。

这是代码:


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
internal class Program
{
static byte[] ConvertWithBuffer/List<string> list/
{
int totalSize = list.Sum/x =&gt; Encoding.UTF8.GetByteCount/x//;
byte[] buffer = new byte[totalSize];

int ix = 0;

foreach /string str in list/
ix += Encoding.UTF8.GetBytes/str, 0, str.Length, buffer, ix/;

return buffer;
}

static byte[] ConvertWithConcat/List<string> list/ { return Encoding.UTF8.GetBytes/String.Concat/list//; }

static byte[] ConvertWithSelectMany/List<string> list/
{
return list
.SelectMany/line =&gt; Encoding.UTF8.GetBytes/line//
.ToArray//;
}

static byte[] ConvertWithString/List<string> input/
{
string fullString = String.Join/String.Empty, input.ToArray///;
return Encoding.UTF8.GetBytes/fullString/;
}

static byte[] ConvertWithStringBuilder/List<string> input/
{
StringBuilder sb = new StringBuilder//;
foreach /string s in input/
sb.Append/s/;

return Encoding.UTF8.GetBytes/sb.ToString///;
}

static IEnumerable<string> CreateList//
{
for /int i = 0; i &lt; 10000000; i++/
yield return i.ToString//;
}

static void Main/string[] args/
{
List<string> strings = CreateList//.ToList//;
Stopwatch stopWatch = Stopwatch.StartNew//;

// warm up
ConvertWithString/strings/;
ConvertWithStringBuilder/strings/;
ConvertWithConcat/strings/;
ConvertWithSelectMany/strings/;
ConvertWithBuffer/strings/;

// testing

stopWatch.Restart//;
ConvertWithString/strings/;
Console.WriteLine/"ConvertWithString {0}ms", stopWatch.ElapsedMilliseconds/;

stopWatch.Restart//;
ConvertWithStringBuilder/strings/;
Console.WriteLine/"ConvertWithStringBuilder {0}ms", stopWatch.ElapsedMilliseconds/;

stopWatch.Restart//;
ConvertWithConcat/strings/;
Console.WriteLine/"ConvertWithConcat {0}ms", stopWatch.ElapsedMilliseconds/;

stopWatch.Restart//;
ConvertWithSelectMany/strings/;
Console.WriteLine/"ConvertWithSelectMany {0}ms", stopWatch.ElapsedMilliseconds/;

stopWatch.Restart//;
ConvertWithBuffer/strings/;
Console.WriteLine/"ConvertWithBuffer {0}ms", stopWatch.ElapsedMilliseconds/;

Console.WriteLine/"press any key..."/;
Console.ReadKey//;
}
}
}


</string></string></string></string></string></string></string></string></string></string></string>

小姐请别说爱

赞同来自:

哦,这样的东西 /

Linq

/?


private byte[] ConvertStringsToBytes/List<string> list/ {
return list
.SelectMany/line =&gt; Encoding.UTF8.GetBytes/line//
.ToArray//;
}


还有另一个机会


private byte[] ConvertStringsToBytes/List<string> list/ {
return Encoding.UTF8.GetBytes/String.Concat/list//;
}


</string></string>

快网

赞同来自:

你可以做这样的事情:


private static byte[] ConvertStringsToBytes/List<string> list/
{
int totalSize = list.Sum/x =&gt; Encoding.UTF8.GetByteCount/x//;
byte[] buffer = new byte[totalSize];

int ix = 0;

foreach /string str in list/
{
ix += Encoding.UTF8.GetBytes/str, 0, str.Length, buffer, ix/;
}

return buffer;
}


我创造一个大
buffer

, 预先计算一般必要的大小 /在
totalSize

/, 然后在循环中填写它
foreach

. 注意使用变量
ix

保存当前位置
buffer

.

此方法之前的优势在于,围绕行或字节阵列没有复制。 编码中的行 UTF8 在缓冲区中录制一次一次
buffer

而没有复制。
</string>

龙天

赞同来自:

List.Add//

允许您只添加一个项目。 你需要施加结果
GetBytes//

在数组中,在数组上执行循环并将每个单独的数组元素添加到列表中。

也许你会找到一种方法来转换数组 GetBytes 列表和使用
AddRange//

...

或者,你省略了一切
List

在您的功能中,只使用数组。 你可能会使用
Array.Resize

, 但这在性能方面不是最好的,因为它将包括很多复制值。 您最好使用一系列字节数组,计算所需的结束大小字节数组,然后将数据从每个内部数组复制到最终数组中。

要回复问题请先登录注册