public static IEnumerable<byte> PackBytes(IEnumerable<byte> sourceBytes, int bits) { var sink = new BitSink(); foreach (byte sourceByte in sourceBytes) { sink.AddBits(sourceByte, bits); while (sink.CanRead(8)) { yield return sink.Read(8); } } if (!sink.IsEmpty) yield return sink.ReadRemaining(); }
public static IEnumerable<byte> UnpackBytes(IEnumerable<byte> sourceBytes, int bits, bool discardRemainder) { var sink = new BitSink(); foreach (byte sourceByte in sourceBytes) { sink.AddByte(sourceByte); while (sink.CanRead(bits)) { yield return sink.Read(bits); } } if (!(sink.IsEmpty || discardRemainder)) yield return sink.ReadRemaining(); }