Simplest way to copy int to byte[]

arrays, c#

Solution

`BitConverter` is quite possibly your friend.

However, that generally returns you a new byte array. It also doesn't let you specify endianness. I have the `EndianBitConverter` class in MiscUtil which has methods to convert primitive types by copying the data directly into an existing byte array.

For instance:

// Copy the bytes from the integer "value" into a byte array
// (buffer) starting at index 5
EndianBitConverter.Little.CopyBytes(value, buffer, 5);

Problem

I have a byte[] and i am iterating through a list of ints(and other data) and i want to copy the int to my byteArray[index*4] How do i do this?

Original source