I'm trying to create a byte[] given some unknown amount of bytes. Here's an example:
ArrayList al = new ArrayList();
al.Add(0xCA);
al.Add(0x04);
byte[] test = (byte[])al.ToArray(typeof(byte));
I'm receiving an error that one or more of the values in the array cannot be converted to a byte. What am I doing wrong here?
Thanks
Try List<byte> and then use ToArray
Either use a generic collection instead of a nongeneric ArrayList, or make sure you are actually using bytes.  0xCA is an int, not a byte.
        ArrayList al = new ArrayList();
        al.Add((byte)0xCA);
        al.Add((byte)0x04);
        byte[] test = (byte[])al.ToArray(typeof(byte));
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With