Lets say In C++ I got code like this..
void * target
uint32 * decPacket = (uint32 *)target;
So in C# it would be like..
byte[] target;
UInt32[] decPacket = (UInt32[])target;
Cannot convert type byte[] to uint[]
How do I convert this memory aligning thing C++ does to arrays to C#?
Well, something close would be to use
Buffer.BlockCopy:Note that the final argument to
BlockCopyis always the number of bytes to copy, regardless of the types you’re copying.You can’t just treat a
bytearray as auintarray in C# (at least not in safe code; I don’t know about in unsafe code) – butBuffer.BlockCopywill splat the contents of thebytearray into theuintarray… 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.