Alternative to BitConverter.ToInt32

Posted by MusiGenesis on Stack Overflow See other posts from Stack Overflow or by MusiGenesis
Published on 2010-03-28T16:53:59Z Indexed on 2010/03/28 17:03 UTC
Read the original article Hit count: 381

Filed under:
|
|

I'm using BitConverter.ToInt32 to pack 3 byte values into an int, like so:

byte R = 0;
byte G = 0;
byte B = 0;
int i = BitConverter.ToInt32(new byte[] { R, G, B, 0 }, 0);

Is there a faster way to do this that doesn't involve the creation of a new int each time? Getting the bytes out of an int is easy:

int i = 34234;
byte B = (byte)(i >> 0);
byte G = (byte)(i >> 8);
byte R = (byte)(i >> 16);

Is there a simple way to reverse this process and use bit-shifting to write the RGB bytes back over an existing int?

© Stack Overflow or respective owner

Related posts about c#

Related posts about bitconverter