Converting List<string> to byte[]

26,295

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();
Share:
26,295

Related videos on Youtube

Jon
Author by

Jon

Updated on April 13, 2020

Comments

  • Jon
    Jon about 4 years

    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

    • Security Hound
      Security Hound about 13 years
      Whats wrong with a simple for loop?
  • JaredPar
    JaredPar about 13 years
    That will give you a List<byte[]> but the OP asked for a byte[]

Related