Method 1: Use the left and right
int converted to byte []:
public byte[] intToBytes(int value)
{
byte[] src = new byte[4];
src[3] = (byte)((value >> 24) & 0xFF);
src[2] = (byte)((value >> 16) & 0xFF);
src[1] = (byte)((value >> 8) & 0xFF);
src[0] = (byte)(value & 0xFF);
return src;
}
byte [] into int:
public int bytesToInt(byte[] src, int offset)
{
int value;
value = (int)((src[offset] & 0xFF)
| ((src[offset + 1] & 0xFF) << 8)
| ((src[offset + 2] & 0xFF) << 16)
| ((src[offset + 3] & 0xFF) << 24));
return value;
}
Method 2: Use BitConverter
int converted to byte []:
public byte[] IntToBitConverter(int num)
{
byte[] bytes = BitConverter.GetBytes(num);
return bytes;
}
byte [] into int:
public int IntToBitConverter(byte[] bytes)
{
int temp = BitConverter.ToInt32(bytes, 0);
return temp;
}