what’s the best way to write the binary representation of an int array (Int32[]) to a Stream?
Stream.Write only accepts byte[] as source and I would like to avoid converting/copying the array to an byte[] (array but instead streaming directly from the ‘original location’).
In a more system-oriented language (a.k.a. C++) I would simply cast the int array to a byte* but as far as I understood this isn’t possible with C# (and moreover, casting byte* to byte[] wouldn’t work out either way)
Thanks
Martin
PS: Actually, I would also like to stream single int values. Does using BinaryConverter.GetBytes() create a new byte array? In this case I extend my question to how to efficiently stream single int values …
The simplest option would be to use
BinaryWriterwrapping your output stream, and callWrite(int)for each of yourintvalues. If that doesn’t use the right endianness for you, you could useEndianBinaryWriterfrom my MiscUtil library.I don’t know of anything built-in to do this more efficiently… I’d hope that the buffering within the stream would take care of it for the most part.