ISO 6346 – Shipping Container Codes

The following link explains how to perform check digit calculations for the validation of shipping container codes.

Below is some code based on the calculation steps shown in the above wikipedia page, it validates the check digit, so the complete container number (including check digit) should be entered in the textbox.

Just drop a button and textbox on a form and wire up the event handlers:

public Dictionary<char, int> AlphabetCodes = new Dictionary<char, int>();
public List<int> PowerOfMultipliers = new List<int>();

private void Form1_Load(object sender, EventArgs e)
    int step = 10;

    //populate dictionary...
    //...create a dictionary entry for all letters of the alphabet using their Ascii value to identify them.
    //if you subtract their ascii value by the value of the first alpha ascii character (in this case 65 for
    //uppercase 'A'), it will give you it's position in the alphabet, Add 10 to this and skip over all multiples
    //of 11 to give you ISO Owner Code numbers for each letter.
    for (int i = 65; i < 91; i++)
        char c = (char)i;
        int pos = i - 65 + step;

        if (c == 'A' || c == 'K' || c == 'U')  //omit multiples of 11.
            step += 1;

        AlphabetCodes.Add(c, pos);  //add to dictionary

    //populate list...
    //create a list of 10, 2^x numbers for calculation.  List should contain 1, 2, 4, 8, 16, 32, 64 etc..
    for (int i = 0; i < 10; i++)
        int result = (int)Math.Pow(2, i);  //power of 2 calculation.
        PowerOfMultipliers.Add(result);  //add to list.

private void button1_Click(object sender, EventArgs e)
    int total = 0;

    if (textBox1.Text.Length == 11)  //container numbers must be 11 characters long.
        for (int i = 0; i < 10; i++)  //loop through the first 10 characters (the 11th is the check digit!).
            if (AlphabetCodes.ContainsKey(textBox1.Text[i]))  //if the current character is in the dictionary.
                total += (AlphabetCodes[textBox1.Text[i]] * PowerOfMultipliers[i]);  //add it's value to the total.
                int serialNumber = (int)textBox1.Text[i] - 48;  //it must be a number, so get the number from the char ascii value.
                total += (serialNumber * PowerOfMultipliers[i]);  //and add it to the total.

        int checkDigit = (int)total % 11;  //this should give you the check digit

        //The check digit shouldn't equal 10 according to ISO best practice - BUT there are containers out there that do, so we'll
        //double check and set the check digit to 0...again according to ISO best practice.
        if (checkDigit == 10)
            checkDigit = 0;

        if (checkDigit != (int)textBox1.Text[10] - 48)  //check digit should equal the last character in the textbox.
            MessageBox.Show("Container Number NOT Valid");
            MessageBox.Show("Container Number Valid");
        MessageBox.Show("Container Number must be 11 characters in length");

3 thoughts on “ISO 6346 – Shipping Container Codes

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s