

That operator shifts the bits in the integer by the specified number of bits.

How do you break an integer in its 4 constituent bytes? You can do it using the bit manipulation operator >.
#RSLOGIX COPY INT TO TWO BYTES 32 BIT#
There are many different ways to do it, but to make it nicer, lets use a new feature of c#: extensions.Īn integer of the 32 bit variety takes 4 bytes, so it will occupy 4 spots in the byte. if (offset > buffer.Length + sizeof(int)) throw new IndexOutOfRangeException()
#RSLOGIX COPY INT TO TWO BYTES CODE#
Using imagination and changing a few lines in the code from step 2 so it could save the data to an existing array: public static unsafe byte GetBytes(int value, int buffer, int offset) You can find the source code of BitConverter.GetBytes(int value) method: public static unsafe byte GetBytes(int value) However, that generally returns you a new byte array. I'll try to summarize some of the previous answers to make something new

(obviously this will copy the int into the first 4 bytes of the array) Solution 3 I have the EndianBitConverter class in MiscUtil which has methods to convert primitive types by copying the data directly into an existing byte array.įor instance: // Copy the bytes from the integer "value" into a byte arrayĮ(value, buffer, 5) ĭo it how the BinaryWriter does it: buffer = (byte) value It also doesn't let you specify endianness. BitConverter is quite possibly your friend.
