{"id":6602,"date":"2022-08-23T09:04:48","date_gmt":"2022-08-23T08:04:48","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=6602"},"modified":"2022-08-23T09:22:52","modified_gmt":"2022-08-23T08:22:52","slug":"c-basic-validate-container-number","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-basic-validate-container-number\/","title":{"rendered":"C# Basic validate container number"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">\/\/ ISO 6346\r\n\r\n\/\/ does NOT validate the \"Check Digit\"\r\n\r\nprivate static Regex containerNumberRegex = new Regex(\"^[a-zA-Z]{3}[uUjJzZ]{1}[0-9]{7}$\");\r\npublic static bool IsValidContainerNumber(string text) => containerNumberRegex.IsMatch(text);\r\n\r\n\r\nIsValidContainerNumber(\" MEDU8707688  \".Trim())\r\n\r\n\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\/\/ Code to get Check digit\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\nprivate static string CharacterValues = \"0123456789A_BCDEFGHIJK_LMNOPQRSTU_VWXYZ\"; \/\/ skip 11th values\r\n\r\n\/\/ short Linq version\r\nprivate static int GetCheckDigit(string containerNumber) \r\n{\r\n    var sum = containerNumber.Select((c, i) => CharacterValues.IndexOf(c) * Math.Pow(2, i)).Sum();\r\n    return (int)(sum - (Math.Floor(sum \/ 11.0) * 11.0));\r\n}\r\n\r\n\/\/ comprehensive version\r\nprivate static int GetCheckDigit(string containerNumber) \r\n{\r\n    var sum = 0.0;\r\n    for (int i = 0; i &lt; containerNumber.Count(); i++)\r\n    {\r\n        char c = containerNumber[i];\r\n        var power = Math.Pow(2, i);\r\n\t\tvar charValue = CharacterValues.IndexOf(c);\r\n\t\tConsole.WriteLine($\"{i}: [{c}] {power,4} * {charValue,4} = {power*charValue,4}\");\r\n        sum += CharacterValues.IndexOf(c) * power;\r\n    }\r\n    var rest =  Math.Floor(sum \/ 11.0) * 11.0;\r\n\tConsole.WriteLine($\"sum-rest: {sum} - {rest} = {sum - rest}\");\r\n    return (int)Math.Floor(sum - rest);\r\n}\r\n\r\nvoid Main()\r\n{\r\n\tConsole.WriteLine(GetCheckDigit(\"CSQU305438\"));\r\n}\r\n\r\n\r\n\/* OUTPUT:\r\n0: [C]    1 *   13 =   13\r\n1: [S]    2 *   30 =   60\r\n2: [Q]    4 *   28 =  112\r\n3: [U]    8 *   32 =  256\r\n4: [3]   16 *    3 =   48\r\n5: [0]   32 *    0 =    0\r\n6: [5]   64 *    5 =  320\r\n7: [4]  128 *    4 =  512\r\n8: [3]  256 *    3 =  768\r\n9: [8]  512 *    8 = 4096\r\nsum-rest: 6185 - 6182 = 3\r\n3\r\n*\/\r\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">What is ISO 6346 (wikipedia definition)<\/h3>\n\n\n\n<p>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).<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/raw.githubusercontent.com\/mixaverros88\/check-digit-iso-6346\/master\/icons\/450px-Containernumber.jpg\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/mixaverros88\/check-digit-iso-6346\/master\/icons\/450px-Containernumber.jpg\" alt=\"preview image\"\/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Step<\/th><th>Container number<\/th><th>Total<\/th><\/tr><\/thead><tbody><tr><td><\/td><td>C S Q U 3 0 5 4 3 8<\/td><td><\/td><\/tr><tr><td>1<\/td><td>13 30 28 32 3 0 5 4 3 8<\/td><td><\/td><\/tr><tr><td><\/td><td>x x x x x x x x x x<\/td><td><\/td><\/tr><tr><td>2<\/td><td>1 2 4 8 16 32 64 128 256 512<\/td><td><\/td><\/tr><tr><td><\/td><td>= = = = = = = = = =<\/td><td><\/td><\/tr><tr><td><\/td><td>13 60 112 256 48 0 320 512 768 4096<\/td><td><\/td><\/tr><tr><td>3<\/td><td>Sum all results from (2)<\/td><td>6185<\/td><\/tr><tr><td>4<\/td><td>Divide (3) by 11 (remainder discarded)<\/td><td>562<\/td><\/tr><tr><td>5<\/td><td>Multiply (4) by 11<\/td><td>6182<\/td><\/tr><tr><td>6<\/td><td>(3) minus (5) = Check Digit:<\/td><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mixaverros88\/check-digit-iso-6346#step-1\"><\/a>Step 1<\/h3>\n\n\n\n<p><em>An equivalent numerical value is assigned to each letter of the alphabet, beginning with 10 for the letter A (11 and multiples thereof are omitted):<\/em><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>0<\/th><th>1<\/th><th>2<\/th><th>3<\/th><th>4<\/th><th>5<\/th><th>6<\/th><th>7<\/th><th>8<\/th><th>9<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><td>5<\/td><td>6<\/td><td>7<\/td><td>8<\/td><td>9<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>A<\/th><th>B<\/th><th>C<\/th><th>D<\/th><th>E<\/th><th>F<\/th><th>G<\/th><th>H<\/th><th>I<\/th><th>J<\/th><th>K<\/th><th>L<\/th><th>M<\/th><\/tr><\/thead><tbody><tr><td>10<\/td><td>12<\/td><td>13<\/td><td>14<\/td><td>15<\/td><td>16<\/td><td>17<\/td><td>18<\/td><td>19<\/td><td>20<\/td><td>21<\/td><td>23<\/td><td>24<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>N<\/th><th>O<\/th><th>P<\/th><th>Q<\/th><th>R<\/th><th>S<\/th><th>T<\/th><th>U<\/th><th>V<\/th><th>W<\/th><th>X<\/th><th>Y<\/th><th>Z<\/th><\/tr><\/thead><tbody><tr><td>25<\/td><td>26<\/td><td>27<\/td><td>28<\/td><td>29<\/td><td>30<\/td><td>31<\/td><td>32<\/td><td>34<\/td><td>35<\/td><td>36<\/td><td>37<\/td><td>38<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mixaverros88\/check-digit-iso-6346#step-2\"><\/a>Step 2<\/h3>\n\n\n\n<p><em>Each of the numbers calculated in step 1 is multiplied by 2position, where position is the exponent to base 2. Position starts at 0, from left to right.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mixaverros88\/check-digit-iso-6346#step-3\"><\/a>Step 3<\/h3>\n\n\n\n<p><em>Sum up all results of Step above<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mixaverros88\/check-digit-iso-6346#step-4\"><\/a>Step 4<\/h3>\n\n\n\n<p><em>Round the result down towards zero i.e. make the result a whole number (integer)<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mixaverros88\/check-digit-iso-6346#step-5\"><\/a>Step 5<\/h3>\n\n\n\n<p><em>Divide them by 11<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mixaverros88\/check-digit-iso-6346#step-6\"><\/a>Step 6<\/h3>\n\n\n\n<p><em>Multiply the integer value by 11<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github.com\/mixaverros88\/check-digit-iso-6346#step-7\"><\/a>Step 7<\/h3>\n\n\n\n<p><em>Subtract result of Step 3 from result of (6): This is the check digit.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-letterofcredit-biz-lc-l-c wp-block-embed-letterofcredit-biz-lc-l-c\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Dg4ilfrNHe\"><a href=\"https:\/\/www.letterofcredit.biz\/index.php\/2019\/11\/04\/what-is-a-container-number-explanations-with-examples\/\">What is a Container Number? Explanations with Examples<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;What is a Container Number? Explanations with Examples&#8221; &#8212; Letterofcredit.biz | LC | L\/C\" src=\"https:\/\/www.letterofcredit.biz\/index.php\/2019\/11\/04\/what-is-a-container-number-explanations-with-examples\/embed\/#?secret=Dg4ilfrNHe\" data-secret=\"Dg4ilfrNHe\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>What is ISO 6346 (wikipedia definition) 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6602","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=6602"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6602\/revisions"}],"predecessor-version":[{"id":6607,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6602\/revisions\/6607"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=6602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=6602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=6602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}