集裝箱編號校驗碼規則及java程式的實現
阿新 • • 發佈:2019-01-30
由於工作原因要接觸到集裝箱編號,便了解了下集裝箱編號最後一位校驗位的規則順便寫了個程式實現,找了一批編號,驗證通過,貼出來做個記錄。
集裝箱校驗碼校驗規則:
- 集裝箱號由4位公司程式碼和7位數字組成(如CBHU3202732),其中第七位數字就是校驗碼。首先將公司程式碼轉換為數字,去掉11及其倍數,連加除以11,其餘數為校驗位。 A=10 B=12 C=13 D=14 E=15 F=16 G=17 H=18 I=19 J=20 K=21 L=23 M=24 N=25 O=26 P=27 Q=28 R=29 S=30 T=31 U=32 V=34 W=35 X=36 Y=37 Z=38 標準箱號構成基本概念:採用ISO6346(1995)標準。
- 第一部分由4位英文字母組成。前三位程式碼 (Owner Code) 主要說明箱主、經營人,第四位程式碼說明集裝箱的型別。列如CBHU 開頭的標準集裝箱是表明箱主和經營人為中遠集運。
- 第二部分由6位數字組成。是箱體註冊碼(Registration Code), 用於一個集裝箱箱體持有的唯一標識。
- 第三部分為校驗碼(Check Digit)由前4位字母和6位數字經過校驗規則運算得到,用於識別在校驗時是否發生錯誤。即第11位數字。 根據校驗規則箱號的每個字母和數字都有一個運算的對應值。箱號的前10位字母和數字的對應值從0到Z對應數值為0到38,11、22、33不能對11取模數,所以要除去。第N位的箱號對應值再分別乘以2的(N-1)次方 (N=1,2,3………..10)例如:箱號為CBHU3202732的集裝箱它的第1位程式碼為C,它的程式碼值=程式碼的對應值×2的(1-1)次方 =13×1=13。類推第2位程式碼為B它的程式碼值=程式碼的對應值×2的(2-1 )次方=12×2=24 以此類推得到箱號前10位程式碼的程式碼值,將前10位的程式碼值乘積累加後對11取模箱號為CBHU3202732的集裝箱前10位箱號的程式碼累加值=4061,取11的模後為2,就是這個箱號第11位的識別碼的數值。以此類推,就能得到校驗碼。
程式碼實現
public class DigitChecker { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String pathname = "D:\\My documents\\ARP\\checkDigitdata.txt"; List<String> data = null; try { data = FileHelper.fileReader(pathname); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < data.size(); i++) { checkDigit(data.get(i)); } // System.out.println("sum is :" + sum); // System.out.println("check digit is " + sum%11); } public static boolean checkDigit(String containerNumber) throws Exception { if (containerNumber == null || containerNumber.trim().length() != 11) { throw new Exception("Not a container number"); } Map<String, Integer> mapofCode = new HashMap<String, Integer>(); mapofCode.put("A", 10); mapofCode.put("B", 12); mapofCode.put("C", 13); mapofCode.put("D", 14); mapofCode.put("E", 15); mapofCode.put("F", 16); mapofCode.put("G", 17); mapofCode.put("H", 18); mapofCode.put("I", 19); mapofCode.put("J", 20); mapofCode.put("K", 21); mapofCode.put("L", 23); mapofCode.put("M", 24); mapofCode.put("N", 25); mapofCode.put("O", 26); mapofCode.put("P", 27); mapofCode.put("Q", 28); mapofCode.put("R", 29); mapofCode.put("S", 30); mapofCode.put("T", 31); mapofCode.put("U", 32); mapofCode.put("V", 34); mapofCode.put("W", 35); mapofCode.put("X", 36); mapofCode.put("Y", 37); mapofCode.put("Z", 38); String constainerCode = containerNumber; int positon = 1; int sum = 0; for (int i = 0; i < constainerCode.length() - 1; i++) { if (mapofCode.containsKey(constainerCode.substring(i, i + 1))) { sum += Double.valueOf(mapofCode.get(constainerCode.substring(i, i + 1))) * Math.pow(2, positon - 1); } else { sum += Double.valueOf(constainerCode.substring(i, i + 1)) * Math.pow(2, positon - 1); } positon++; } int checkdigit = sum % 11 % 10; System.out.println("check container number:" + constainerCode + ";get check digit is " + checkdigit + ";origin check digit is " + constainerCode.substring(constainerCode.length() - 1, constainerCode.length())); boolean result = checkdigit == Integer .valueOf(constainerCode.substring(constainerCode.length() - 1, constainerCode.length())); return result; }