Writing an Int array to a stream in .NET.

What is the best way to write a binary representation of an int ( Int32[]) array in Stream?

Stream.Writeaccepts only byte[]as a source, and I would like to avoid converting / copying the array to byte[](an array, but instead streaming directly from the "source location").

In a more system-oriented language (aka C ++), I just passed an int array to byte*, but as I understand it, this is not possible with C # (and moreover, casting byte*in byte[]will not work anyway)

thank

Martin

PS: In fact, I would also like to convey single values int. Does the BinaryConverter.GetBytes()new byte array use? In this case, I’ll talk about how to efficiently pass single values int...

+3
source share
2 answers

The simplest option would be to BinaryWriterwrap your output stream and call Write(int)for each of your values int. If you are not using the correct orientation for you, you can use MiscUtilEndianBinaryWriter from my library .

I don’t know anything built-in to do this more efficiently ... I hope that buffering inside the stream will take care of this for the most part.

+9
source

System.Array and System.Int32 have SerializableAttribute and therefore support default serialization in extractable format.

http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx

:

http://msdn.microsoft.com/en-us/library/aa904194(VS.71).aspx

+3

Source: https://habr.com/ru/post/1763462/


All Articles