开发者

Converting List<string> to byte[]

开发者 https://www.devze.com 2023-02-24 07:26 出处:网络
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.ForEachDepends on which encoding you want

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

精彩评论

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