Evyatar Evyatar - 1 month ago 14
C# Question

Convert from string array to byte array

I'm using C#.

I have a string array as follow:

"1,2,3,4,5,..."

I'm trying to convert the string array to byte array as follow
[]{1,2,3,4,5,...}

What is the best way to do that?

Thanks.

Answer

Try using Linq:

 string source = "1,2,3,4,5";

 byte[] result = source
   .Split(',')
   .Select(item => byte.Parse(item))
   .ToArray();
Comments