decimalToBinary function

Uint8List decimalToBinary(
  1. int size,
  2. String s
)

Convert an unsigned decimal number in s to a bignum @param size bignum size (bytes)

Implementation

Uint8List decimalToBinary(int size, String s) {
  var result = Uint8List(size);
  for (var i = 0; i < s.length; ++i) {
    var srcDigit = s.codeUnitAt(i);
    if (srcDigit < '0'.codeUnitAt(0) || srcDigit > '9'.codeUnitAt(0)) {
      throw 'invalid number';
    }
    var carry = srcDigit - '0'.codeUnitAt(0);
    for (var j = 0; j < size; ++j) {
      var x = result[j] * 10 + carry;
      result[j] = x;
      carry = x >> 8;
    }
    if (carry != 0) {
      throw 'number is out of range';
    }
  }
  return result;
}