public static ulong UnpackDirect(Packer packer) { var size = packer.ReadByte(); var res = new Int64Bytes() { b1 = (size >= 1 ? packer.ReadByte() : byte.MinValue), b2 = (size >= 2 ? packer.ReadByte() : byte.MinValue), b3 = (size >= 3 ? packer.ReadByte() : byte.MinValue), b4 = (size >= 4 ? packer.ReadByte() : byte.MinValue), b5 = (size >= 5 ? packer.ReadByte() : byte.MinValue), b6 = (size >= 6 ? packer.ReadByte() : byte.MinValue), b7 = (size >= 7 ? packer.ReadByte() : byte.MinValue), b8 = (size >= 8 ? packer.ReadByte() : byte.MinValue) }; return(res.value); }
/// <summary> /// Write an Int64 (long) value to a byte array /// </summary> /// <param name="bytes">The array to write it in</param> /// <param name="start">The index at which to start writing it</param> /// <param name="value">Value to write</param> /// <exception cref="ArgumentException">Thrown if the value would not fit in the byte array</exception> public static void WriteInt64(byte[] bytes, int start, long value) { if (8 > bytes.Length - start) { throw new ArgumentException("The value does not fit in bytes", nameof(value)); } var b = new Int64Bytes { Int64 = value }; bytes[start + 0] = b.B0; bytes[start + 1] = b.B1; bytes[start + 2] = b.B2; bytes[start + 3] = b.B3; bytes[start + 4] = b.B4; bytes[start + 5] = b.B5; bytes[start + 6] = b.B6; bytes[start + 7] = b.B7; }
public static void PackDirect(Packer packer, ulong obj) { var b = new Int64Bytes() { value = obj }; byte size = 8; if (b.b8 == 0) { --size; if (b.b7 == 0) { --size; if (b.b6 == 0) { --size; if (b.b5 == 0) { --size; if (b.b4 == 0) { --size; if (b.b3 == 0) { --size; if (b.b2 == 0) { --size; if (b.b1 == 0) { --size; } } } } } } } } packer.WriteByte(size); if (size >= 1) { packer.WriteByte(b.b1); } if (size >= 2) { packer.WriteByte(b.b2); } if (size >= 3) { packer.WriteByte(b.b3); } if (size >= 4) { packer.WriteByte(b.b4); } if (size >= 5) { packer.WriteByte(b.b5); } if (size >= 6) { packer.WriteByte(b.b6); } if (size >= 7) { packer.WriteByte(b.b7); } if (size >= 8) { packer.WriteByte(b.b8); } }