public static int ToInt32(params byte[] v) { var r = 0; var len = v.Length; if (len > 4) { len = 4; } for (var i = 0; i < len; i++) { r |= v[i] << 8 * (len - i - 1); } return r; } public static byte[] ToBytes(int v) { var len = 0; for (var i = 0; i < 4; i++) { if (v >> 8 * i < 255) { len = i + 1; break; } } var bs = new byte[len]; for (var j = 0; j < bs.Length; j++) { bs[j] = (byte)(v >> (bs.Length - j - 1) * 8); } return bs; }
时间: 2024-11-05 12:19:21