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));
Run Code Online (Sandbox Code Playgroud)
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
Ste*_*ham 11
尝试List<byte>然后使用ToArray
要么使用泛型集合而不ArrayList是非泛型集合,要么确保实际使用字节.0xCA是一个int,而不是一个byte.
ArrayList al = new ArrayList();
al.Add((byte)0xCA);
al.Add((byte)0x04);
byte[] test = (byte[])al.ToArray(typeof(byte));
Run Code Online (Sandbox Code Playgroud)