Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the check digit according to ISO 6346

How to implement the check digit algorithm specified in ISO 6346:2022 Freight containers — Coding, identification and marking?

See how is the check digit of a container calculated?

This is a self-answered question.

like image 520
Damien Bezborodow Avatar asked Nov 30 '25 17:11

Damien Bezborodow


1 Answers

This calculates the check digit as per ISO 6346:2022:

function iso6346CheckDigit(csc) {
  // Remove any non-alphanumeric characters and convert to upper-case.
  csc = csc.toUpperCase().replace(/[^A-Z0-9]+/g, '');

  // Check if length fits requirements.
  if (csc.length < 10 || csc.length > 11)
    return NaN;

  // Calculate check digit.
  var sum = 0;
  for (let i = 0; i < 10; i++) {

    // Map letters to numbers.
    let n = csc.charCodeAt(i);
    n -= n < 58 ? 48 : 55;

    // Numbers 11, 22, 33 are omitted.
    n += (n-1) / 10;

    // Sum of all numbers multiplied by weighting.
    sum += n << i;
  }

  // Modulus of 11, and map 10 to 0.
  return sum % 11 % 10;
}

console.log(iso6346CheckDigit('ZEPU 003725 [5]')); // 5
console.log(iso6346CheckDigit('GVTU 300038 [9]')); // 9
console.log(iso6346CheckDigit('TEXU 307007 [9]')); // 9
console.log(iso6346CheckDigit('GABU 307007 [0]')); // 0
console.log(iso6346CheckDigit('BAAD 10000.....')); // NaN

The above has been published on GitHub.

like image 187
Damien Bezborodow Avatar answered Dec 02 '25 06:12

Damien Bezborodow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!