由于工作原因,最近项目中接触到关于集装箱编号校验的问题,搜索网上资料,借鉴以下两个网址总结一下。
java实现:http://blog.youkuaiyun.com/benkwok/article/details/54971387
javaScript实现:http://blog.youkuaiyun.com/ttgzs/article/details/1566011
集装箱校验码校验规则:
- 集装箱号由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位的识别码的数值。以此类推,就能得到校验码。
java方法实现代码:
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;
}
本人是前端开发工程师,所以使用以下方法
JavaScript方法实现:










































































































































前台调用方法如下




















