public static void Main()
int[] testcases = { int.MinValue, int.MaxValue, 1, -1, 0 };
foreach (var testcase in testcases)
byte[] buffer = new byte[4];
Int32ToBytes(testcase, buffer, 0);
int output = BytesToInt32(buffer, 0);
Console.WriteLine($"{testcase,13} --> {output,13}");
public static void Int32ToBytes(int input, byte[] target, int offset = 0)
target[offset] = (byte)(input >> 24);
target[offset + 1] = (byte)(input >> 16);
target[offset + 2] = (byte)(input >> 8);
target[offset + 3] = (byte)input;
public static int BytesToInt32(byte[] source, int offset = 0)
return source[offset] << 24 | source[offset + 1] << 16 | source[offset + 2] << 8 | source[offset + 3];