convert method
Converts encoded array of numbers with bit-length of source to an array
of numbers with bit-length of target. The encoded array will be treated
as a sequence of bits to convert.
If the encoded array contains negative numbers or numbers having more than
the source bits, it will be treated as the end of the input sequence.
After consuming all of input sequence, if there are some non-zero partial word remains, it will throw FormatException.
Implementation
@override
List<int> convert(covariant List<int> encoded) {
final sb = source;
final tb = target;
if (sb < 2 || sb > 64) {
throw ArgumentError.value(source, 'source', 'should be between 2 to 64');
}
if (tb < 2 || tb > 64) {
throw ArgumentError.value(target, 'target', 'should be between 2 to 64');
}
int p, s, t, l, n;
l = encoded.length * sb;
var out = Uint8List(l ~/ tb);
// generate words from the input bits
p = n = t = l = 0;
s = 1 << (sb - 1);
s = s ^ (s - 1);
for (final x in encoded) {
if (x < 0 || x > s) break;
p = (p << sb) ^ x;
t = (t << sb) ^ s;
n += sb;
while (n >= tb) {
n -= tb;
out[l++] = p >>> n;
t >>>= tb;
p &= t;
}
}
// p > 0 means that there is a non-zero partial word remaining
if (p > 0) {
throw FormatException('Invalid length or non-zero trailing bits');
}
if (l < out.length) {
return out.sublist(0, l);
}
return out;
}