Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting List<string> to byte[]

How can I take a List and turn it into a byte array.

I thought there might be some clever LINQ options for it but am unsure eg/List.ForEach

like image 472
Jon Avatar asked Apr 12 '11 15:04

Jon


1 Answers

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();
like image 71
JaredPar Avatar answered Oct 11 '22 02:10

JaredPar