How do I convert byte array to UInt32 array?
Well, something close would be to use Buffer.BlockCopy
:
uint[] decoded = new uint[target.Length / 4];
Buffer.BlockCopy(target, 0, decoded, 0, target.Length);
Note that the final argument to BlockCopy
is always the number of bytes to copy, regardless of the types you're copying.
You can't just treat a byte
array as a uint
array in C# (at least not in safe code; I don't know about in unsafe code) - but Buffer.BlockCopy
will splat the contents of the byte
array into the uint
array... leaving the results to be determined based on the endianness of the system. Personally I'm not a fan of this approach - it leaves the code rather prone to errors when you move to a system with a different memory layout. I prefer to be explicit in my protocol. Hopefully it'll help you in this case though.
You can have the cake (avoid allocations) and eat it too (avoid iterations), if you're willing to move to the dark side.
Check out my answer to a related question, in which I demonstrate how to convert float[] to byte[] and vice versa: What is the fastest way to convert a float[] to a byte[]?