pub fn encode_bits(bits: usize, bytes: &[u8]) -> StringExpand description
Encode a fixed number of bits (arbitrary length) as a Base44 string with optimal length.
This function treats the input bytes as a big integer containing exactly bits bits
and encodes it using the minimum number of Base44 characters required.
§Optimal Encoding
For N bits, the optimal Base44 length is ceil(N * log(2) / log(44)):
- 103 bits → 19 chars (2^103 < 44^19)
- 104 bits → 20 chars (2^104 < 44^20)
- 256 bits → 47 chars (2^256 < 44^47)
This is more efficient than byte-pair encoding when the bit count doesn’t align with byte boundaries, saving up to 5% space for certain bit lengths.
§Performance Optimization
For small bit counts, native integer types are used for better performance:
- bits ≤ 64: uses u64 (fastest)
- bits ≤ 128: uses u128 (fast)
- bits > 128: uses BigUint (fallback)
§Arguments
bits- Number of significant bits (must be > 0). Bytes are read in little-endian order.bytes- Input bytes in LSB-first order (matching typical bit-packing schemes).
§Example
// Encode 103 bits (13 bytes with top byte using 7 bits)
let data = [0u8; 13];
let encoded = qr_base44::encode_bits(103, &data);
assert_eq!(encoded.len(), 19); // Optimal length for 103 bits