convert method

  1. @override
Uint8List convert(
  1. covariant List<int> encoded
)
override

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
Uint8List convert(List<int> encoded) {
  final table = alphabet;
  final tlen = table.length;
  final pad = padding;
  final len = encoded.length;
  final sb = source;
  if (sb < 2 || sb > 64) {
    throw ArgumentError.value(source, 'source', 'should be between 2 to 64');
  }

  int i, x, y, p, n, l;
  var out = Uint8List((len * sb) >>> 3);

  p = n = l = 0;
  for (i = 0; i < len; ++i) {
    y = encoded[i];
    if (y == pad) break;
    if (y < 0 || y >= tlen || (x = table[y]) < 0) {
      throw FormatException('Invalid character $y at $i');
    }
    p = (p << sb) ^ x;
    n += sb;
    while (n >= 8) {
      n -= 8;
      out[l++] = p >>> n;
      p &= (1 << n) - 1;
    }
  }

  for (; i < len; ++i) {
    y = encoded[i];
    if (y != pad) {
      throw FormatException('Invalid character $y at $i');
    }
  }
  if (p > 0) {
    throw FormatException('Invalid length or non-zero trailing bits');
  }
  if (l < out.length) {
    return out.sublist(0, l);
  }
  return out;
}