I need to write an integer to a byte array such that leading zeros are omitted and the bytes are written in big endian order.
Example:
int    original = 0x00123456;
byte[] encoded  = Encode(original);  //  == new byte[] { 0x12, 0x34, 0x56 };
int    decoded  = Decode(encoded);   //  == 0x123456
My Decode method:
private static int Decode(byte[] buffer, int index, int length)
{
    int result = 0;
    while (length > 0)
    {
        result = (result << 8) | buffer[index];
        index++;
        length--;
    }
    return result;
}
I'm struggling to come up with an Encode method that doesn't require a temporary buffer or reverses the bytes after writing them in little endian order. Can anyone help?
private static int Encode(int value, byte[] buffer, int index)
{
}