集装箱编号校验码规则及java/JavaScript的实现

由于工作原因,最近项目中接触到关于集装箱编号校验的问题,搜索网上资料,借鉴以下两个网址总结一下。

java实现:http://blog.csdn.net/benkwok/article/details/54971387

javaScript实现:http://blog.csdn.net/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)标准。 
    1. 第一部分由4位英文字母组成。前三位代码 (Owner Code) 主要说明箱主、经营人,第四位代码说明集装箱的类型。列如CBHU 开头的标准集装箱是表明箱主和经营人为中远集运。
    2. 第二部分由6位数字组成。是箱体注册码(Registration Code), 用于一个集装箱箱体持有的唯一标识。
    3. 第三部分为校验码(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方法实现:

//  JavaScript Document
function  changechar(str)
{
   
    
if ((str=="a")||(str=="A"))
    
return 10;
    
else if ((str=="b")||(str=="B"))
    
return 12;
        
else if ((str=="c")||(str=="C"))
    
return 13;
        
else if ((str=="d")||(str=="D"))
    
return 14;
        
else if ((str=="e")||(str=="E"))
    
return 15;
        
else if ((str=="f")||(str=="F"))
    
return 16;
        
else if ((str=="g")||(str=="G"))
    
return 17;
        
else if ((str=="h")||(str=="H"))
    
return 18;
        
else if ((str=="i")||(str=="I"))
    
return 19;
        
else if ((str=="j")||(str=="J"))
    
return 20;
        
else if ((str=="k")||(str=="K"))
    
return 21;
        
else if ((str=="l")||(str=="L"))
    
return 23;
        
else if ((str=="m")||(str=="M"))
    
return 24;
        
else if ((str=="n")||(str=="N"))
    
return 25;
        
else if ((str=="o")||(str=="O"))
    
return 26;
        
else if ((str=="p")||(str=="P"))
    
return 27;
        
else if ((str=="q")||(str=="Q"))
    
return 28;
        
else if ((str=="r")||(str=="R"))
    
return 29;
        
else if ((str=="s")||(str=="S"))
    
return 30;
        
else if ((str=="t")||(str=="T"))
    
return 31;
        
else if ((str=="u")||(str=="U"))
    
return 32;
        
else if ((str=="v")||(str=="V"))
    
return 34;
        
else if ((str=="w")||(str=="W"))
    
return 35;
        
else if ((str=="x")||(str=="X"))
    
return 36;
        
else if ((str=="y")||(str=="Y"))
    
return 37;
        
else if ((str=="z")||(str=="Z"))
    
return 38
        
else 
    
return -1000;
//if ((str=="a")||(str=="A"))
//
return 10;
//
else if (str=="b")
//
return 1;
//
else 
//
return 7;
}

function  GetCntr(strcntr)
{
    
var num = new Array(10)
    
for (i=0;i<11;i++)
    
{
        num[i]
=0;
    }

    test
=strcntr;//prompt("请输入需校验的集装箱编码","◎◎◎◎×××××××")
    len=test.length;
    
if (len != 11)
    
{
        alert(
"请重新输入11位的集装箱编码!");
        
return false;
    
//location.reload();
    }

    
else 
    
{
        exp
=/^[A-Za-z]{4}d{7}$/g;
        
if (!exp.test(test)) 
        
{
        alert(
"集装箱编码格式不正确,前四位应为字母,后七位为数字,请重新输入!");
        
return false;
        
//location.reload();
        }


        left
=test.substr(0,4);
        right
=test.substr(4,7);
        testnum
=test.substr(10,1);
    
        char1
=test.substr(0,1);
        char2
=test.substr(1,1);
        char3
=test.substr(2,1);
        char4
=test.substr(3,1);
        
//箱号字头
        num[0]=changechar(char1);
        num[
1]=changechar(char2);
        num[
2]=changechar(char3);
        num[
3]=changechar(char4);

        
//序列号
        num[4]=test.substr(4,1);
        num[
5]=test.substr(5,1);
        num[
6]=test.substr(6,1);
        num[
7]=test.substr(7,1);
        num[
8]=test.substr(8,1);
        num[
9]=test.substr(9,1);
        
//校验数字
        num[10]=test.substr(10,1);
/*
for (i=0;i<11;i++){
document.write(num[i]);
document.write("<br>");
}
*/

    sum
=num[0]+num[1]*2+num[2]*4+num[3]*8+num[4]*16+num[5]*32+num[6]*64+num[7]*128+num[8]*256+num[9]*512;  
result
=sum%11;
//document.write("集装箱编码为:" + test + "<br><br>")
    if (result!= num[10])
        
{
            
//document.write("<font color=blue>校验码错误!");
            //document.write("正确的校验码为:</font><font color=red>" + result + "</font>");
            alert("校验码错误!正确的校验码为:" + result);
            
return false;
        }

        
else
            
{
                document.write(
"<font color=red>正确</font><br>");
            
return true;
            }

            
    }
    
}

前台调用方法如下

 

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312"   />
< title > 无标题文档 </ title >
< script  language ="javascript"  type ="text/jscript"  src ="cntr.js" >
    
</ script >

</ head >

< body >
< form  id ="form1"  name ="form1"  method ="post"  action ="" >
  
< label >
  
< input  type ="text"  name ="textfield"  id ="textfield"  onkeypress =""   />
  
< input  type ="submit"  name ="Submit"  value ="提交"  onclick ="return GetCntr(document.form1.textfield.value)"   />
  
</ label >

</ form >
</ body >
</ html >


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值