D
D
Daniel2020-12-25 02:20:32
C++ / C#
Daniel, 2020-12-25 02:20:32

How to convert int array to byte array?

array int[]? Whether it is possible to transform in one action, instead of each element to read. An array is just a region in memory. Changing the type, I don’t change anything, the same 4 bytes remain in the same place.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-12-25
@vabka

Maybe something like.

var array = new[] {1, 2, 3, 4};
var span = new Span<int>(array);
var bytes = MemoryMarshal.AsBytes(span);
var byteArray = bytes.ToArray(); //тут будет копирование.

If copying is undesirable, then you can try using pointers and unsafe.

K
kirill69, 2020-12-25
@kirill69

I'm certainly a noob but maybe this will help
https://docs.microsoft.com/ru-ru/dotnet/csharp/pro...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question