public static byte[] ToByteArray(this BitArray bits) { var ret = new byte[(bits.Length - 1) / 8 + 1]; bits.CopyTo(ret, 0); return ret; }