public static byte[] Int32ToBytes(int intValue)
{
byte[] bytes = new byte[4];
bytes[0] = (byte)(intValue & 0x000000FF);
bytes[1] = (byte)((intValue & 0x0000FF00) >> 8);
bytes[2] = (byte)((intValue & 0x00FF0000) >> 16);
bytes[3] = (byte)((intValue & 0xFF000000) >> 24);
return bytes;
}
public static int BytesToInt32(byte[] bytes)
{
long longValue = (uint)bytes[0] & 0x000000FF
| ((uint)bytes[1] << 8) & 0x0000FF00
| ((uint)bytes[2] << 16) & 0x00FF0000
| ((uint)bytes[3] << 24) & 0xFF000000;
return (int)longValue;
}
public static byte[] Int16ToBytes(short shortValue)
{
byte[] bytes = new byte[2];
bytes[0] = (byte)(shortValue & 0x000FF);
bytes[1] = (byte)((shortValue & 0xFF00) >> 8);
return bytes;
}
public static short BytesToInt16(byte[] bytes)
{
short longValue = (short)((uint)bytes[0] & 0x00FF
| ((uint)bytes[1] << 8) & 0xFF00);
return longValue;
}
转载于:https://www.cnblogs.com/musicfans/archive/2012/06/13/2819315.html