How can I take a List and turn it into a byte array.
I thought there 开发者_StackOverflow中文版might be some clever LINQ options for it but am unsure eg/List.ForEach
Depends on which encoding you want to use to convert the string
to a byte[]
but here's a sample for ASCII. It can be substituted for pretty much any encoding type
List<string> data = ...
byte[] dataAsBytes = data
.SelectMany(s => Text.Encoding.ASCII.GetBytes(s))
.ToArray();
with a simple foreach loop:
(pseudocode)
List<byte[]> bytes = new List<byte[]>();
ForEach string el in somelist
{
byte[] arr;
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
arr = encoding.GetBytes(el);
bytes.add(arr);
}
精彩评论