Derivated from ISO 6346
ISO 6346 is an international standard covering the coding, identification and marking of intermodal (shipping) containers used within containerized intermodal freight transport. The standard establishes a visual identification system for every container
that includes a unique serial number (with check digit), the owner, a country code, a size, type and equipment category as well as any operational marks. The standard is managed by the International Container Bureau (BIC).
(ref https:s://en.wikipedia.org/wiki/ISO_6346)
Container Code Format ISO6346 |
|---|
| Format | AAAX0000009 |
|---|---|
| AAA | Owner Code The owner code consists of three capital letters of the Latin alphabet to indicate the owner or principal operator of the container. Such code needs to be registered at the Bureau International des Conteneurs in Paris to ensure uniqueness worldwide. |
| X | Equipment Category Identifier The equipment category identifier consists of one of the following capital letters of the Latin alphabet: J : detachable freight container-related equipment U : freight containers Z : trailers and chassisUnder the ISO code, then, only J, U and Z are in use—the reefer container is identified by means of the size type code. |
| 000000 | Serial Number The serial number consists of 6 numeric digits, assigned by the owner or operator, uniquely identifying the container within that owner/operator's fleet. |
| 9 | Check Digit The check digit consists of one numeric digit providing a means of validating the recording and transmission accuracies of the owner code and serial number. |
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; // .Net Framework 4.0 namespace TechScales { internal static class ContainerCodeValidation { /// <summary> /// check alphanumeric container code, /// code4: /// J = EQUIPMENTS & TOOLS, /// U = Universal, /// Z = CHASSIS & TRAILER /// </summary> /// <param name="arg"></param> /// <returns></returns> public static string GetCheckDigitContainerID(this string arg) { if (string.IsNullOrWhiteSpace(arg)) return string.Concat(arg, "☺"); // remove space, make uppercase var id = arg.Trim().Replace(" ", string.Empty).ToUpper(); // make 10 chars or take 10 chars id = id.Length < 10 ? id.PadRight(10, '0') : (id.Length > 10 ? id.Substring(0, 10) : id); // alphabet, numeric and space only foreach (var s in id) { if (!"ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789".Contains(s)) return arg; } // valid code position 4th var code4 = "JUZ"; for (var c = 0; c < "AAAX".Length; c++) { if (c < 3 && !"ABCDEFGHIJKLMNOPQRSTUVWXYZ".Contains(id[c])) return arg; if (c == 3 && !code4.Contains(id[c])) return arg; } for (var c = 0; c < 10; c++) { if (!"0123456789".Contains(id[c])) return arg; } int numer = 0, azFrom = 10, azTo = 39; var codAZ = Enumerable.Range(azFrom, (azTo - azFrom)) .Where(n => n % 11 > 0 && n <= azTo) .ToArray(); var multi = new int[] { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 }; var codes = new List<int>(); try { var i = 0; foreach (var s in id) { numer = 0; if (i < 4) { numer = codAZ[(int)s - 65]; } else { int.TryParse(s.ToString(), out numer); } numer *= multi[i]; codes.Add(numer); i++; } numer = codes.Sum(); numer -= (int)Math.Floor(numer / 11d) * 11; id = string.Concat(id, " ", numer); } catch { id = string.Concat(id, "-1"); } codAZ = null; multi = null; codes.Clear(); codes = null; return id; } /// <summary> /// extension: containe code validation /// </summary> /// <param name="arg"></param> /// <returns></returns> public static bool IsValidContainerCode(this string arg) { var ret = ""; return IsValidContainerCode(arg, ref ret); } /// <summary> /// extension: containe code validation /// </summary> /// <param name="arg"></param> /// <param name="result">filtered string</param> /// <returns></returns> public static bool IsValidContainerCode( this string arg, ref string result) { var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // check valid string, check string length must be 11 var v0 = string.Join(string.Empty, // make uppercase arg.ToUpper() // remove empty char .Replace(" ", string.Empty) // alphabet and numeric only .Where(f => valid.Contains(f)) // as array for string.join purpose .ToArray()); if (v0.Length != 11) { return false; } // do checking methode, get result var v1 = v0.GetCheckDigitContainerID().Replace(" ", ""); result = v0; return v0 == v1; } } } |
Test |
|---|
| Immediate Window |
| ?"CSQU3054383".IsValidContainerCode() |
| true |
bool yes = "CSQU3054383".IsValidContainerCode(); // return true |