what would be best way to convert object using rust from bigint (BigInt) to bits (BitArray<217>) also in reverse (example below)
using binary to decimal calculator I verified by hand that bigint and bits equate
let bigint = BigInt::parse_bytes("141644482300309102636663083870634002744809361056209271964506585197".as_ref(), 10);
to
let bits = BitArray::new( [1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1]);
crates ref.
for BigInt: https://crates.io/crates/num-bigint (num-bigint = "0.4.3")
for BitArray: https://crates.io/crates/bitarray (bitarray = "0.10.0")
Couple of misconceptions:
BigInt) to a size known at compile time (BitArray)BigIntis signed, but at no point during your conversion you consider signedness. You probably want to useBigUintinstead if you want to ignore signedness.BitArrayfor comparison only consists of1and0values.bitarray::BitArray, however, is meant as bytes, meaning, each value is 8 bits, valued from0to255. If you convert it, the value is actuallyBitArray::new([1, 88, 81, 147, 230, 162, 120, 203, 210, 232, 153, 239, 115, 92, 222, 74, 147, 18, 216, 202, 55, 207, 181, 126, 72, 92, 248, 109])and28long.bitarray::BitArraydoes not seem to be able to iterate over it bitwise, so I don't know how useful this library is for you. The fact that it forces compile time size is also not compatible with your usecase. You should probably choose a different library. The entire concept of a "bit array" is probably not what you want, you probably want a "bit vector" instead with a runtime size.That said,
bitarrayconsists of packed bits, meaning 8 bits per value (or more). If you want a pureVec<bool>, you don't need any of this, you can directly convert it to that:Not that the value you give seems to be little-endian, while this one is big-endian.
Here is an even shorter version, utilizing the crate
bitvec:Or, if you prefer little endian:
Note that while
bitsis packed, you can still iterate over it bitwise. If you want to convert it to au8vector, do: