/// <summary>
/// int转byte
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
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;
}
/// <summary>
/// byte转int
/// </summary>
/// <param name="src"></param>
/// <param name="offset"></param>
/// <returns></returns>
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;
}
}
C# int和byte之间的互转
最新推荐文章于 2024-07-03 15:46:07 发布