Easiest way to drop a numerical integer into a byte array WITHOUT using Bitconverter

HI All, I’m creating a homebrew compression algorithm to reduce the size of an image. However, I don’t want to use bitconverter to put pixels into the graphic array because it creates a ton of really small byte arrays that eat up my memory. Is there anything like memcpy in TinyCLR to allow me to simply mirror the value into the graphics array directly without an interim array?

Bit shift and masking?

Use >> with multiples of 8 to get to the bytes. Assign that value masked with 0xff to your array.

I am assuming that when you say “the value”, that it is some signed or unsigned 32 bit value.

for example:
a[i] = (byte)(source >> 24 & 0xff);
a[i + 1] = (byte)(source >> 16 & 0xff);
a[i + 2] = (byte)(source >> 8 & 0xff);
a[i + 3] = (byte)(source & 0xff);

On a memory level, when you cast a 32 bit integer into a byte, does it just truncate the first 24 bytes and take the first 8? Sorry if this is a simple question, i just didn’t even think about this solution.

I haven’t experimented recently, and I don’t recall whether casting to byte also masks the bits. The casting above may not be necessary - it’s just there in case the result of shift and mask is something other than byte. Casting to byte when you haven’t masked might throw an overflow exception, or might just mask off the low byte. I can’t recall offhand, but the basic mechanism above is a suitable replacement for BitConverter. It can maybe be optimized further.

Yes, it does.
I use this way all the time to convert a 16bit int into 2 bytes for transmitting on a can bus


Well then by using the cast and dropping the mask operation ( & 0xff) you can probably shave off a few instructions and win some performance.