public static void WriteBytesToMemStream(this BitArray bitArray, MemoryStream ms) { if (!bitArray.Check16BitRule()) { throw new Exception("Bit array does not obey 16 bit rule"); } BinaryWriter bw = new BinaryWriter(ms); bw.Write(bitArray.ToBytes()); }
public static byte[] ToBytes(this BitArray bitArray) { if (!bitArray.Check16BitRule()) { throw new Exception("Bit array does not obey 16 bit rule"); } byte[] array = new byte[bitArray.Count / 8]; bitArray.CopyTo(array, 0); return(array); }
public static BitArray MergeWith(this BitArray bitArray, BitArray otherBitArray) { if (!bitArray.Check16BitRule()) { throw new Exception("Bit array does not obey 16 bit rule"); } if (!otherBitArray.Check16BitRule()) { throw new Exception("Other bit array does not obey 16 bit rule"); } byte[] mergedBytes; using (MemoryStream ms = new MemoryStream()) { bitArray.WriteBytesToMemStream(ms); otherBitArray.WriteBytesToMemStream(ms); mergedBytes = ms.ToArray(); } return(new BitArray(mergedBytes)); }