N
N
Neks_Minor2015-12-26 21:59:01
C++ / C#
Neks_Minor, 2015-12-26 21:59:01

How to convert List element to byte[] array?

First I declare a list : Then I fill it with data:
List<string> K = new List<string>();

K.Add("FirstString");
K.Add("SecondString");
K.Add("ThirdString");

When I want to take the i-th element of a list and convert it to an array of bytes,
byte[] tmp = K[i].SelectMany(s => Encoding.Default.GetBytes(s)).ToArray();

VS throws an error:
The most appropriate overloaded method for "System.Text.Encoding.GetBytes(char[])" has multiple invalid arguments

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2015-12-26
@dmitryKovalskiy

Have you tried s.ToCharArray()?

A
AM5800, 2015-12-27
@AM5800

You are calling SelectMany on string. Therefore s is of type char. But GetBytes is expecting a string.
For some reason it seems to me that you wanted to write like this:
byte[] tmp = K.SelectMany(s => Encoding.Default.GetBytes(s)).ToArray();
Or so
byte[] tmp = Encoding.Default.GetBytes(K[i]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question