0

I can convert string[] to byte[] like in the following code:

 byte[] k = {255, 150, 155, 255, 255, 255, 255, 255, 255, 255, 55, 55, 15, 55, 155, 55};
 string st = BitConverter.ToString(Array.ConvertAll(k, Convert.ToByte));     
 byte[] kk = new byte[16];
 string[] sts = st.Split('-');            
 for (int i = 0; i < 16; i++)
 {
    kk[i] = Convert.ToByte(sts[i], 16);
 }

But I can't do the same with LINQ like in the code below:

Array.ConvertAll(sts,item=>(byte) Convert.ToByte(item, 16))

How do I make this work in LINQ?

Why is it not working in the "Immediate Window" of Visual Studio?

Lambda expressions do not work in "Immediate" and "Watch" windows.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

0

Your code does work. Maybe you forgot a semicolon:

 var a = Array.ConvertAll(sts, s => Convert.ToByte(s, 16));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nikolay Fedorov
  • 387
  • 2
  • 7
0

Here is the elegant solution to convert from a byte array (or double array) to a string and from string to byte/double array. :)

double[] k = {255, 150, 155, 25, 2, 55, 66};
string st = BitConverter.ToString( Array.ConvertAll(k, Convert.ToByte));

And from string to double array...

double[] kk = Array.ConvertAll(st.Split('-'), s => (double) Convert.ToByte(s, 16));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131