I need a quick way of converting a pair of integers to a byte array. If it were a single value, I could use the following:
private static unsafe byte[] ToBytes(int value)
{
var bytes = new byte[4];
fixed (byte* pointer = bytes)
{
*(int*)pointer = value;
}
return bytes;
}
However I can't quite figure out how you'd do the same for a pair of integers. The following which I tried doesn't work but should help to show what I'm aiming for
private static unsafe byte[] ToBytes(int value1, int value2)
{
var bytes = new byte[8];
fixed (byte* pointer = bytes)
{
*(int*)pointer = value1;
*(int*)pointer + 4 = value2;
}
return bytes;
}
(I'm aware I can do the same using BitConverter, but I want to compare the performance of the two)