baseToDec function

String baseToDec(
  1. String toBeConverted,
  2. int base
)

Convert toBeConverted, the String representation of a value with a certain base (E.g. 16 for hexadecimal, 2 for binary, etc.), to another String expressed with the decimal base.

Implementation

String baseToDec(String toBeConverted, int base) {
  toBeConverted = toBeConverted.toUpperCase();

  var regExp = getBaseRegExp(base);

  if (!regExp.hasMatch(toBeConverted)) return '';

  int conversion = 0;
  int len = toBeConverted.length;
  for (int i = 0; i < len; i++) {
    int unitCode = toBeConverted.codeUnitAt(i);
    int digit;
    if (unitCode >= 65 && unitCode <= 70) {
      // from A to F
      digit = unitCode - 55;
    } else if (unitCode >= 48 && unitCode <= 57) {
      // from 0 to 9
      digit = unitCode - 48;
    } else {
      return '';
    }
    conversion = conversion * base + digit;
  }
  return conversion.toString();
}