Probably the fastest way to do this is to use unsafe and fixed constructs. Here is an example:
ulong[] ulongs = new ulong[64]; byte[] bytes = new byte[512]; unsafe { fixed (ulong* src = ulongs) { byte* pb = (byte*)src; for (int i = 0; i < bytes.Count(); i++) { bytes[i] = *(pb + i); } } }
Remember to use the /unsafe compiler when compiling the code above.
Edit: here is the version of the first code fragment that works on my machine much faster:
unsafe { fixed (ulong* src = ulongs) { fixed (byte *dst = bytes) { ulong* pl = (ulong*)dst; for (int i = 0; i < ulongs.Count(); i++) { *(pl + i) = *(src + i); } } } }
source share