convert method
Converts input array of numbers with bit-length of source to an array
of numbers with bit-length of target. The input array will be treated
as a sequence of bits to convert.
After consuming all of input sequence, if there are some non-zero partial word remains, 0 will be padded on the right to make the final word.
Implementation
@override
Uint8List convert(List<int> input) {
final table = alphabet;
final pad = padding;
final tb = target;
final len = input.length;
if (tb < 2 || tb > 64) {
throw ArgumentError.value(target, 'target', 'should be between 2 to 64');
}
// number of output words = ceil(total input bits / tb)
int n = len << 3;
int outLen = (n + tb - 1) ~/ tb;
if (pad != null) {
final low = tb & -tb;
final period = low < 8 ? 8 ~/ low : 1;
outLen = ((outLen + period - 1) ~/ period) * period;
}
var out = Uint8List(outLen);
// regroup the input bytes and map each word through the alphabet
int i, p, l;
p = n = l = 0;
for (i = 0; i < len; ++i) {
p = (p << 8) | (input[i] & 0xFF);
n += 8;
while (n >= tb) {
n -= tb;
out[l++] = table[p >>> n];
p &= (1 << n) - 1;
}
}
// if a partial word remains, left-align it, zero-padding the low bits
if (n > 0) {
out[l++] = table[p << (tb - n)];
}
// fill the remaining slots with the padding character
if (pad != null) {
while (l < outLen) {
out[l++] = pad;
}
}
return out;
}