generateErrorCorrection static method

String generateErrorCorrection(
  1. String dataCodewords,
  2. int errorCorrectionLevel
)

Generates the error correction codewords according to 4.10 in ISO/IEC 15438:2001(E).

@param dataCodewords the data codewords @param errorCorrectionLevel the error correction level (0-8) @return the String representing the error correction codewords

Implementation

static String generateErrorCorrection(
  String dataCodewords,
  int errorCorrectionLevel,
) {
  final k = getErrorCorrectionCodewordCount(errorCorrectionLevel);
  final e = List.filled(k, 0);
  final sld = dataCodewords.length;
  for (int i = 0; i < sld; i++) {
    final t1 = (dataCodewords.codeUnitAt(i) + e[e.length - 1]) % 929;
    int t2;
    int t3;
    for (int j = k - 1; j >= 1; j--) {
      t2 = (t1 * _EC_COEFFICIENTS[errorCorrectionLevel][j]) % 929;
      t3 = 929 - t2;
      e[j] = ((e[j - 1] + t3) % 929);
    }
    t2 = (t1 * _EC_COEFFICIENTS[errorCorrectionLevel][0]) % 929;
    t3 = 929 - t2;
    e[0] = (t3 % 929);
  }
  final sb = StringBuffer();
  for (int j = k - 1; j >= 0; j--) {
    if (e[j] != 0) {
      e[j] = (929 - e[j]);
    }
    sb.writeCharCode(e[j]);
  }
  return sb.toString();
}