开发者

Converting List<string> to byte[]

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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜