convertUPCEtoUPCA static method

String convertUPCEtoUPCA(
  1. String upce
)

Expands a UPC-E value back into its full, equivalent UPC-A code value.

@param upce UPC-E code as string of digits @return equivalent UPC-A code as string of digits

Implementation

static String convertUPCEtoUPCA(String upce) {
  final upceChars = List.generate(6, (index) => upce.codeUnitAt(index + 1));
  // upce.getChars(1, 7, upceChars, 0);
  final result = StringBuffer();
  result.write(upce[0]);
  final lastChar = upceChars[5];
  switch (lastChar) {
    case 48: //'0'
    case 49: //'1'
    case 50: //'2'
      result.write(String.fromCharCodes(upceChars.getRange(0, 2))); // 0, 2
      result.writeCharCode(lastChar);
      result.write('0000');
      result.write(String.fromCharCodes(upceChars.getRange(2, 5))); // 2, 3
      break;
    case 51: //'3'
      result.write(String.fromCharCodes(upceChars.getRange(0, 3))); // 0, 3
      result.write('00000');
      result.write(String.fromCharCodes(upceChars.getRange(3, 5))); // 3, 2
      break;
    case 52: //'4'
      result.write(String.fromCharCodes(upceChars.getRange(0, 4))); // 0, 4
      result.write('00000');
      result.writeCharCode(upceChars[4]);
      break;
    default:
      result.write(String.fromCharCodes(upceChars.getRange(0, 5))); // 0, 5
      result.write('0000');
      result.writeCharCode(lastChar);
      break;
  }
  // Only append check digit in conversion if supplied
  if (upce.length >= 8) {
    result.write(upce[7]);
  }
  return result.toString();
}