Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

440
Vistas
C# in-place cast `int[]` array to `byte[]` array

I have an array of some type (short[], int[], or similar base type). I want to send it through a stream, so I need it to be byte[]. The standard solution seems to be creating a new byte[] array, and use BitConverter. The problem with this approach is performance and memory usage - it requires allocating all new memory, copying into the new memory, and then writing the byte[] to the stream.

I very much want to use the original backing memory of the int[] array as a byte[] array so I can send it in-place. I am willing to enforce the requirement that the sending and receiving system must be the same endianness, for the sake of this very obvious and very significant performance difference.

Is there some way to treat an array of int[] or similar, as a byte[] in-place?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If you can use Span<T> (.net core 3+):

void Send(int[] data)
{
    ReadOnlySpan<byte> byteRef = MemoryMarshal.AsBytes(data.AsSpan());
    _stream.Write(byteRef);
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda