I tried to use the List.ConvertAll method and failed. What I am trying to do is convert a List
to byte[]
I copped out and went
To use the ConvertAll method you can do the following...
Assuming that you have a list of ints that are really byte values and you do not actually want the bytes required to make up an int, i.e. byte[][]
:
public static class Utility {
public static byte IntToByte(int i) {
if(i < 0)
return (byte)0;
else if(i > 255)
return (byte)255;
else
return System.Convert.ToByte(i);
}
}
... to convert ...
byte[] array = listOfInts.ConvertAll(
new Converter(Utility.IntToByte) ).ToArray();
or you could use an anonymous delegate...
byte[] array = listOfInts.ConvertAll( new Converter(
delegate(int i) {
if(i < 0)
return (byte)0;
else if(i > 255)
return (byte)255;
else
return System.Convert.ToByte(i);
})).ToArray();