C# int和byte之间的互转
1.方式一:手动位移 public static 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; } public static 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; } 2.方式二:使用BitConverter public static int IntToBitConverter(int num) { int temp = 0; byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组 temp = BitConverter.ToInt32(bytes, 0);//将字节数组内容再转成int32类型 return temp; }