JavaScript : How to Calculate the Check Digit according to ISO 6346

Advertisements

Introduction

The International Organization for Standardization (ISO) has defined a standard for the identification of shipping containers known as ISO 6346. Part of this standard involves a check digit, which is used to ensure the accuracy of the container number and prevent errors in container tracking and documentation. In this blog post, we will learn how to calculate the check digit according to ISO 6346.

The Check Digit Calculation Algorithm

The check digit is calculated using a simple algorithm that involves assigning numeric values to each character in the container number and performing mathematical operations. Here are the steps to calculate the check digit:

  1. Take the first ten characters of the container number, excluding the check digit itself.
  2. Assign numeric values to the letters using the ISO 6346 standard. A = 10, B = 12, C = 13, and so on, up to Z = 35.
  3. Multiply each digit by its corresponding weight. The weights are assigned from right to left as follows: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512.
  4. Sum up the weighted values.
  5. Divide the sum by 11.
  6. Take the remainder of the division and subtract it from 11.
  7. If the remainder is 0, the check digit is 0. If the remainder is 1, the check digit is represented by the letter X. Otherwise, the remainder is the check digit.

An Example Calculation in JavaScript

Let’s calculate the check digit for a sample container number: ABCU1234567. Here’s how you can do it in JavaScript:

function calculateCheckDigit(containerNumber) {
  const weights = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512];
  const numericValues = [];
  
  // Remove the check digit and convert letters to numeric values
  const containerPrefix = containerNumber.slice(0, -1).toUpperCase();
  for (let i = 0; i < containerPrefix.length; i++) {
    const charCode = containerPrefix.charCodeAt(i);
    if (charCode >= 65 && charCode <= 90) {
      numericValues.push(charCode - 55);
    } else {
      numericValues.push(Number(containerPrefix[i]));
    }
  }
  
  // Calculate the weighted sum
  let sum = 0;
  for (let i = 0; i < numericValues.length; i++) {
    sum += numericValues[i] * weights[i];
  }
  
  // Calculate the check digit
  const remainder = sum % 11;
  const checkDigit = remainder === 0 ? '0' : remainder === 1 ? 'X' : (11 - remainder).toString();
  
  return checkDigit;
}

const containerNumber = 'ABCU1234567';
const checkDigit = calculateCheckDigit(containerNumber);

console.log('Container Number:', containerNumber);
console.log('Check Digit:', checkDigit);</code></pre>
Container Number: ABCU1234567
Check Digit: 9

Conclusion

Calculating the check digit according to ISO 6346 is a straightforward process that involves assigning weights to characters, performing mathematical operations, and deriving the check digit. By following the steps outlined in this blog post and using the provided JavaScript example, you can accurately calculate the check digit for any given container number. Implementing the check digit verification helps ensure the integrity of container identification and facilitates efficient container tracking and logistics operations.

Leave a ReplyCancel reply